Getting MX record from DNS
Programming April 18th, 2007First 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.
Some other posts that you may like :
Coffee timeSilent growth in Google's monolopy
10 Things make you die earlier
3 Easy Steps To Transform Your Old XP Desktop to a Fresh Vista Looking Desktop!
Intelligent Surface from Microsoft
Wireless Pen Mouse
PQI Veils 256GB 2.5-inch SSD Drive
Starcraft II Trailer
Information Engineer - A New Career
What is .NET Framework 3.0 ?
Recent Comments