Getting MX record from DNS
Programming No Comments »First of all, if you downloaded and tried the Tiny Mailer (Download link is below), you should be realized that program making a direct connection to Mail Exchange servers and sending your mail. But how to obtain the ip address of the mail exchange server from the given receiver’s mail address.The trick is there, if you know the ip address of the mx server then you can make a direct connection as a normal SMTP Client and send your mail. Below there is a small code piece that shows how you can do this process in C# .Net. This little piece of code takes advantage of using “nslookup” tool. It opens the “nslookup” in the background make a query for a given host then returns the output to a string, then the first mx record is parsed and returned from the function.
public string getmxserver(string hostname)
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.UseShellExecute = false;
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = “nslookup”;
proc.StartInfo.Arguments = “-type=mx ” + hostname;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
StreamReader pout = proc.StandardOutput;
string s = pout.ReadToEnd();
string[] snew = s.Split(’,');
string[] snew2 = snew[1].Split(’=');
string[] snew3 = snew2[1].Split(’ ‘);
string[] snew4 = snew[1].Split();
string mxserver = snew4[4];
return mxserver;
}
As you can see it is the simplest way of Getting MX record. I hope this would be helpful to those who are intended in coding SMTP client.




Recent Comments