Click here to Skip to main content
15,898,373 members
Articles / Web Development / ASP.NET
Article

Three Ways of E-Mail Address Validation - Regex, SMTP, POP3

Rate me:
Please Sign up or sign in to vote.
3.00/5 (13 votes)
9 Feb 2007CPOL3 min read 63.4K   930   63   3
Article about the technology of checking email address validation

Introduction

When we have received a task from the manager to create a system for sending different kinds of messages for thousands of our customers, first of all we have to validate the email addresses which are stored in our database.

So how do you reliably determine if an email address is correct or incorrect before sending messages? The answer is – you can't. But you can reduce the count of wrong addresses and eventually do it after sending.

This is an article regarding this issue.

Three Ways of Validation

I have found two ways of validation in the MSDN library. The first way is by using the Regex class from System.Text.RegularExpressions namespace for spell checking.

C#
Regex mailRegex=new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

But it checks only the spelling format of addresses. It can't answer if an address really exists or not.

The second way is to check the answer of MAIL server by using the SMTP protocol. The problem is that .NET Framework does not have the classes for this protocol, but you can find them here.

The name of the project is MailChecker.zip.

In this way, you can check the address before sending. It gets the connection with the SMTP Server, then sends the message to the server for validation. Then the server does the validation and answers if it's incorrect, it returns the error message. But actually it always checks only the spelling and existing server name. It means it does not check the name of the email box. bill_geitz@microsoft.com or guyuyuy8hg@microsoft.com are always correct, but really do not exist. However, the server answers that the addresses exist.

Most SMTP servers will accept mail addressed to just about anyone in their domain, and only later figure out that the user does not exist. That means that whatever app you use to send mail will almost never know that there is a problem.

Only the third way can answer exactly whether the address is correct or not. This is the way of sending messages and receiving bounced messages.

After sending a message, if the address does not exist, the MAIL Server sends the answer: bounced messages with special subject. It is somewhat like "Delivery Status" or "Failure Notice" message. Our task now is to retrieve this message by using the POP3 protocol and recognize what is wrong. Actually this issue was described in another article on The Code Project: "Using Bounced E-mail Messages to Clean Your Address List".

But the other problem appears here. .NET 2.0 does not have a class for working with POP3. It is ridiculous but…

In the article stated above, the author uses the library EasyMail which does the entire job with POP3.

It is not convenient, because we cannot see legally what is inside and need to use the LicenseKey of the library. I have decided to create my own class for this task. For this, I use the other article from The Code Project: "A POP3 Client in C# .NET".

It's a pretty interesting library but it has a few bugs. I did not fix them. I have cut some functionality. It is O.K. for our task because validation is not necessary to parse all messages. You can see it in the example.

That is all. After reading bounced messages, we can change the information in the database and never send anything anymore to those addresses.

History

  • 9th February, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Australia Australia
Originally Dmitry is a Software Developer from Russia.Currently he is working for a Australian Company. He has been involved in programming for over 6 years.

Comments and Discussions

 
GeneralThank you, was very usefull Pin
Member 81408-May-08 2:52
Member 81408-May-08 2:52 
GeneralCareful about cleaning your list from bouncing email... Pin
Jcmorin9-Feb-07 15:58
Jcmorin9-Feb-07 15:58 
GeneralRe: Careful about cleaning your list from bouncing email... Pin
Dmitry Kikhtev10-Feb-07 19:45
Dmitry Kikhtev10-Feb-07 19:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.