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.
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. firstname.lastname@example.org or email@example.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.
- 9th February, 2007: Initial post