Validating and Email Address

Well I am doing it again, writing a form that validates a TextBox to figure out if it is an email address. If I had a ten cents for every time I have written this same for code in ISAPI, IDC/HTX, Classic ASP, ASP.NET and other languages I would be rich.

 

Once you have the input you need to validate that it really is an email address. There is no way to know for sure, since email is a forward only protocol. Some of the same I have done this in the past include:

  1. Check to make sure that there is a @ , which is pretty lame.
  2. Use a regular expression to check the email address, a little better.

Tonight I was thinking that it would be handy to get the MX record of the domain for the host of the email address. This way I would know if the host name accepted email (or at least the DNS administrator for that domain thought it accepted email) and I would also know that the domain exists and was returning DNS responses.

 

So search on the Internet for a C# query that would get me the list of MX records for the domain I found a article called: Build a C# DNS MX (Mail Exchange) Record Query Class  written by Peter A. Bromberg! I have actually met this guy he is pretty cool.

 

My code looks like this:

// WWB: Check To Make Sure The Email Address Parses
MailAddress mailAddress = null;
try
{
    mailAddress = new MailAddress(people.People_Email);
}
catch
{
    return;
}

// WWB: Check To Make Sure There Is a SMTP Server To Recieve The Email Address
if (DnsMx.GetMXRecords(mailAddress.Host).Length==0)
{
    return;
}

// WWB: Success 
{6230289B-5BEE-409e-932A-2F01FA407A92}

Comments

Popular posts from this blog

Simple WP7 Mango App for Background Tasks, Toast, and Tiles: Code Explanation

Yet once more into the breech (of altered programming logic)

How to convert SVG data to a Png Image file Using InkScape