SIGN UP MEMBER LOGIN:    
ARTICLE

Verify Email Online

Posted by Jean Paul Articles | .NET 4.5 March 14, 2011
Last week I was working on an application to verify an email online. So I thought of sharing the information with .Net community as well.
Reader Level:
Download Files:
 


Last week I was thinking of an application that can verify email online. So I thought of sharing the information with the .Net community as well.

The Problem

Let us think of a database of hundreds of emails which were collected over years. The problem here is that some of the email exists, some do not. So while sending notifications to clients, the returned invalid user mail messages require a lot of manual work to clear them. Our idea is to verify whether an email exists before sending the mail. Everything should be automatic and less time consuming.

The Solution

There were two solutions evolved explained below:

  1. Automate nslookup and SMTP commands using C#
  2. Use WebClient automation to verify email using a website

Above all, allow multi-threading to be used so that the delays can be shared.

How do the SMTP commands work?

It is worth learning how we can send emails manually using SMTP commands over telnet. For eg: Our interested email id is supercat@gmail.com. We can verify the email using the following steps:
  1. Find the domain name from email. In the above case it is gmail.com
  2. Find the mail exchange server name from the domain name. Use command nslookup for achieving this: nslookup –q=mx gmail.com

    email1.gif

    The highlighted line shows that the gmail-smtp-in.l.google.com should be the high preference mail server. The actual preference should be treated as lower for a higher preference value.
     
  3. Connect to the mail server using port 25 and verify the email address. The commands would be:

    telnet gmail-smtp-in.l.google.com 25
    MAIL FROM: <sender@yourdomain.com>
    RCPT TO: <supercat@gmail.com>

    (If the email exists then the mail server will return 250 – if it doesn't then the value will be 550.)

Note: Our aim is to automate the commands through C#.

How does email verification with website work?

There are many online services which allows us to verify emails online. For eg:

email2.gif

We can enter the email id and click the Verify button on website. If the email id exists it will return Result: Ok as reply.

Note: Our aim is to automate the form filling and submitting through C#. But the site only allows 20 emails to be verified per hour. So we need to find another site providing the service.

Implementation

As we analyzed both cases of verifying email, shall we jump into the implementation part? For implementing the MX Server lookup part we are using the following method:

public IList<MXServer> GetMXServers(string domainName)
{
    string command = "nslookup -q=mx " + domainName;
    ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command);

    procStartInfo.RedirectStandardOutput = true;
    procStartInfo.UseShellExecute = false;

    procStartInfo.CreateNoWindow = true;

    Process proc = new Process();
    proc.StartInfo = procStartInfo;
    proc.Start();
    string result = proc.StandardOutput.ReadToEnd();

    if (!string.IsNullOrEmpty(result))
        result = result.Replace("\r\n", Environment.NewLine);

    IList<MXServer> list = new List<MXServer>();
    foreach (string line in result.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
    {
        if (line.Contains("MX preference =") && line.Contains("mail exchanger ="))
        {
            MXServer mxServer = new MXServer();
            mxServer.Preference = Int(GetStringBetween(line, "MX preference = ", ","));
            mxServer.MailExchanger = GetStringFrom(line, "mail exchanger = ");

            list.Add(mxServer);
        }

       return list.OrderBy(m => m.Preference).ToList();
}


The EmailVerifier Class

The core method in EmailVerifier class is CheckExists() which takes an email as argument. The method used for verification (SMTP or Website) is determined by the parameter passed to EmailVerifier constructor. The body of CheckExists() is given below:

public bool CheckExists(string email)
{
    if (_useSMTPMethod)
        return IsFormatValid(email) && IsExists_SMTPMethod(email);
    else
        return IsFormatValid(email) && IsExists_WebClientMethod(email);
}

For checking multiple emails the following method can be used:

public IList<string> CheckExists(IList<string> emailList)
{
    _validList = new List<string>();
    _count = 0;

    foreach (string email in emailList)
    {
        ParameterizedThreadStart threadStart = new ParameterizedThreadStart(CheckEmailExistsThreaded);
        Thread thread = new Thread(threadStart);

        thread.Start(email);
    }

    // Wait for completion
    while (_count < emailList.Count)
    {
        Thread.Sleep(100);
    }

    return _validList;
}


The above method will validate the emails passed and returns only the existing emails. The method will be using threads to gain the waiting time usage.

Unit Testing

The associated project contains unit tests for testing the core methods. All the Email validation methods and multi-threading tests were executing using it.

email3.gif

Note

Please ensure the antivirus/firewall settings are not hindering the socket communications.

Test Application in Windows

A small test application is also attached for providing the user interface to enter the email address and verify it.

email4.gif

Login to add your contents and source code to this article
share this article :
post comment
 

Hi jean,Thank You for this project

Posted by hadi dehghani Mar 29, 2011

Hi Jean, Yes i like this article.

Posted by Vijay Yadav Mar 15, 2011

Thank You Vijay for the good words.. Hope you like it :)

Posted by Jean Paul Mar 14, 2011

Hi Jean, Thanks for sharing here!!!!:)

Posted by Vijay Yadav Mar 14, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor