Saturday, April 11, 2009

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 everytime 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 @ -- lame. 2) Regular expression. -- 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 came up with: http://www.eggheadcafe.com/articles/20050129.asp Thanks 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}

0 comments:

Post a Comment