Validating Your Users Email Addresses

Michael OssouIt’s really simple. If you can’t contact your users, you have a problem. For better or worse, your primary way of staying in contact with your users is still an email address. So you want to make sure you have a valid email address for them that not only is good during signup, but long after. That means periodically you should check on your data.

The only way to be 100% certain the email address is valid would be to actually email the customer. We’ll get to that shortly, but first let’s look at another option.

How can we programmatically determine if an email address is valid with a reasonable amount of certainty? One option would be to test the email address using a library like ASPNETMX. ASPNETMX can test the validity of an email address by essentially doing four things for us:

  1. Check the Syntax of the email address.
  2. Make sure the domain has a valid MX record associated with it.
  3. Make sure the correct SMTP server exists by connecting to it.
  4. Verify the email address is valid by attempting to send a message and stopping at the last moment

It’s the last part that’s interesting. Let’s manually have an SMTP conversation using telnet and look at what’s going on:

telnet sm11.internetmailserver.net 25
Server: 220 sm11.internetmailserver.net
Me: HELO MyLocalComputerName
SERVER: 250 sm11.internetmailserver.net Hello
Me: MAIL FROM:[email protected]
Server: 250 OK < thesender @michaelossou.com> Sender ok
Me: RCPT TO:[email protected]
Server: 250 OK < therecipient @michaelossou.com> Recipient ok

When sending a regular message, this conversation would continue. The subject, body, etc. of the message are defined and then the message is sent. ASPNETMX goes through only the above steps and disconnects from the server. It’s interested only in the response the server will issue for the recipients email address. If the server replies ‘Ok’, it considers the email address valid. If the response is not ‘Ok’, it will report that to us as well.

Let’s look at a simple example of a console application that takes an email address and uses ASPNETMX to validate the email address.

using aspNetMX;

namespace AspNetMxSingleTest
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                MXValidate.LoadLicenseKey("1234-1234-1234-1234");
                MXValidate mx = new MXValidate();
                MXValidateLevel lvl = MXValidateLevel.NotValid;
                mx.CheckGreylisting = true;
                mx.CacheMXTimeOut = new TimeSpan(1, 0, 0);
                mx.LogInMemory = true;

                string emailAddress;
                Console.WriteLine("Enter Email Address:");
                emailAddress = Console.ReadLine();
                lvl = mx.Validate(emailAddress, MXValidateLevel.Mailbox);
                Console.WriteLine(lvl.ToString());
                Console.WriteLine(mx.GetLog());

            }

        }
    }
}

Now, let’s look at the results.

ASPNETMX Results

A more complete option would probably be to actually send an email. The idea being that when it’s time to send a newsletter or some other communication, we use the bounced messages to flag our bad email addresses.

Services like MailGun and SendGrid make this a pretty painless process as they offer APIs to deal with such events. SendGrid even has a Nuget Package to drop their bits into your Visual Studio project. These aren’t the cheapest options, but if you read our post regarding mass email last week, you will see a lot goes into the services they offer.

7 thoughts on “Validating Your Users Email Addresses

  1. Why doesn’t DiscountASP provide such an api to deal with bounced emails? For example if an email sent through your smtp server (localhost) bounces back it would be nice to receive a notification (in an usable format) from DiscountASP so that we can update our database accordingly.

    Services like MailGun and SendGrid are not free. It seems to me logical that DiscountAsp offers such a service too (as an addon for example) given that you already allow your customers to send emails through your smtp server. As a customer we wouldn’t need to use a third party service and you would make more money….

    Amazon already does that (their ‘bounced message’ notification is sent in json format): http://aws.amazon.com/about-aws/whats-new/2012/06/26/amazon-ses-announces-bounce-and-complaint-notifications/

    1. Services like MailGun and SendGrid are not free.

      No they aren’t, because maintaining mass email services is expensive. It’s a completely different animal than “normal” email services such as we provide, and it’s not a field we’re looking to expand in to. If we did offer bulk emailing service as an add-on, it would likely be a partnership to offer a discount on an existing service such as MailGun or SendGrid.

      An API to do bounce management isn’t possible because we don’t have a bounce management system on the production mail servers to provide an API to. But a future article such as this one on dealing with bounce management is a possibility.

  2. this blog post is good of course but all your code examples in your support pages only mention ‘localhost’ so it looks a bit contradictory

    Well, you can look at it as contradictory or you can look at it as supplementary.

    I hear what you’re saying, and in the future we may well have a tie-in with an external email service. As for integrating bounce management instructions into the Knowledge Base, we haven’t done so because no one asks for it. This is the first bounce management conversation we’ve had with a customer in as long as I can remember, so it’s safe to say that the demand simply isn’t there.

    But you’re right, bounce management is a great and – for some – essential thing. You may see more about it here in the blog, and again, there’s always an integration possibility in the future. We can only work on so many projects, improvements and enhancements though, so we tend to focus on those that will be of the greatest benefit to the greatest number of users.

  3. We use ASPNETMX very successfully.

    Every New or Changed eMail is put through their Validate Process triggered by an asp CustomValidator.

    The result only changes by the user we do not know about causes a bounceback.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.