C# ASP.NET SQL SERVER

Normalizing Email Addresses

I was going to write a post here describing this function, why I wrote it, and what it does. But then I re-read the comments that I'd added to the function and the examples I'd included in the code and said "Hey, this is perfectly documented and doesn't need any further explanation."

/// <summary>
/// Takes an email address and converts it to a normalized base form.
/// This is needed for some email addresses such as gmail where you can
/// add dots anywhere in the name as well as a + sign and tag anything
/// extra on to the email address.
/// Example email addresses to normalize:
/// is.this.an.email@gmail.com
/// is.this.an.email+extrainfo@gmail.com
/// isthisanemail+extrainfo@gmail.com
/// These should all normalize to:
/// isthisanemail@gmail.com
/// </summary>
/// <param name="emailAddress">Any email address</param>
/// <returns>A modified/normalized email address or the original string</returns>
static public string NormalizeEmailAddress(string emailAddress)
{
    // So far we only know about gmail.com that does this
    if(emailAddress.Contains("@gmail.com"))
    {
        string[] parts = emailAddress.Split('@');
        // If there are more than 2 @'s in the address then this is an
        // invalid email address so don't try and do anything to it.
        if (parts.Count() == 2)
        {
            string[] beforeAtParts = parts[0].Split('+');
            return beforeAtParts[0].Replace(".","") + "@" + parts[1];
        }
    }

    return emailAddress;
}

Ironically I love this gmail feature and I use it all the time and I think that most people use it for the good. Unfortunately there's a small element of the population who will use this for multiple registrations on sites so that they can troll and spam so I am doing this conversion as a preventitive measure. Note that on the sites that I've implemented this on I've still allowed the users to register with any address that they want. However, when comparing a registration attempt against a site that does not allow registrations with the same email address then this will prevent multiple registrations by the same person.

» Similar Posts

  1. ASP.NET MVC DropDownList from Enum
  2. Identify duplicate IP address use to the public
  3. Optimizing a custom Trim() function in C#

» Trackbacks & Pingbacks

    No trackbacks yet.
Trackback link for this post:
http://guyellisrocks.com/trackback.ashx?id=162

» Comments

  1. Daniel Wong avatar

    Thanks, Guy. It seems like there's alot more you have to do to normalize an email address (esp if it isn't gmail).

    Also, how did you find out that gmail does this?

    Daniel Wong — July 22, 2009 8:01 PM
  2. guy ellis avatar

    A friend of mine (Bill Brown) blogged about it. Very few people know about this feature of gmail.

    guy ellis — July 22, 2009 9:32 PM
  3. Mike avatar

    This is a really stupid feature. An alias on a user's domain is fine

    anyaddress@myowndomain.com

    but part alias on an isp/mail domain is asking for trouble. It's irresponsible.

    Mike — December 6, 2009 6:41 PM
  4. Mike avatar

    Thanks for highlighting this!

    Mike — December 6, 2009 6:42 PM
  5. guy ellis avatar

    Mike - I disagree, I think that this is a great feature of Google's. It allows me to register a unique email address at each site on the web that I register at.

    If I suddenly start receiving spam from some unknown source then my email address will tell me which site I registered at that sold my email address or was a front for spam address collection.

    These unique email addresses also allow me to set-up filters on my email client to delete or categorize mail from each source.

    guy ellis — December 6, 2009 6:50 PM

» Leave a Comment