Click here to Skip to main content
Click here to Skip to main content

Parsing Email Addresses using an RFC822 Compliant Address Validator

, 10 Oct 2002 BSD
Rate this:
Please Sign up or sign in to vote.
This article, first presents a working parser and then goes on to describe the mechanics and problems faced validating email addresses.

A word of warning

This class was developed as a proof of concept/R&D so there's no exception handling. I seriously advise implementing some try {...} catch {...} statements before you use it in a production environment.

Introduction

If your writing an application that will in some point in it's lifecycle deal with an email address in a little more detail (IE your writing an SMTP server, or a webmail application), then you'll most probably want to validate and check the correctness of the address before allowing anywhere near your implementation and methods.

In todays fast paced society, email has become a wildly used medium for communication. Addressing of email relies on one standard. This standard is RFC 822, and is one of the more confusing specifications to implement to implement due to it's sheer complexity and size.

Very generally speaking, an email address, according to RFC 822, can be broken into 3 primary parts.

  • Quoted Identifier - This is the part of the address that gives the greater amount of identification of the email mailbox owner. It's usually the full name of the sender/recipient.
  • Local Part - This is the actual mailbox/alias name hosted on the destination server. It's the part before the @ sign.
  • Domain - This is typically the FQDN (Fully Qualified Domain Name) where the mailbox identified by the local part is hosted.

This of course is very general when it comes to the actual RFC. There's a lot more detail in the specification.

The only way that I know to validate an email address correctly so that it traverses the Internet and does not fail someplace along the line is ensuring that the email address (and any parts thereof) comply with RFC822. To do this I've ported Jeffrey E.F. Friedl's RFC 822 compliant regular expression to C# and implemented a small wrapper for it.

The EmailAddress class, upon initialisation, builds the regular expression and passes it to a static Regex object. Why is it static? Because the regex is so big. Check out the regex.txt file in the download and you'll see why. It's huge. Were you to type this code out you would get a serious case of backslashitis and a headache. Thankfully Jeffrey builds the regex slowly, bit by bit.

Quick usage sample

Here's a quick piece of code to demonstrates use of the EmailAddressclass.

class EmailTest
{
    [STAThread]
    static void Main(string[] args)
    {
        Console.Write("Email Address to validate: ");
        string email = Console.ReadLine();

        EmailAddress emailAddr = new EmailAddress(email);
        if (emailAddr.IsValid)
        {
            Console.WriteLine("Mailbox: {0}",
                emailAddr.Mailbox);

            Console.WriteLine("Quoted Name: {0}",
                emailAddr.QuotedString);

            Console.WriteLine("Local Part: {0}",
                emailAddr.LocalPart);

            Console.WriteLine("Domain: {0}",
                emailAddr.Domain);
        }
        else 
        {
            Console.WriteLine("Email Address is invalid");
        }
        Console.WriteLine("Press <enter> to exit");
        Console.ReadLine();
    }
}

The EmailAddress class has 5 public fields and 1 public method. The fields are:

  • Mailbox
  • QuotedString
  • LocalPart
  • Domain
  • IsValid

Mailbox is the full email address submitted to the regex. QuotedString is the actual quoted string of the mail address ("s included), LocalPart identifies the mailbox of the mail address and Domain is the domain. The IsValid field returns true when the class validates correctly

You can either provide the constructor for the class with and email address to validate, or initialise the class and call the Parse() method supplying the email address.

Parse() also returns the result of the validation

This code was tested on Windows XP using the .NET 1.0 (SP1) SDK.

History

  • October 11, 2002 - Initial posting

License

This article, along with any associated source code and files, is licensed under The BSD License

Share

About the Author

Richard Smith
Web Developer
United Kingdom United Kingdom
Working in the highly competitive and uniquely challenging environment of the newspaper industry, you'll find Richard tinkering with some part of his employers ad booking or content management system.
 
In his spare time, he enjoys playing with the latest programming gadetry in some assemblance of reasearch.
 
When not stuck infront of a computer you'll normally find this geek in a library, in the local multiplex or more probably down the pub.
 
He accepts donations of beer and free books Smile | :)
 

Comments and Discussions

 
GeneralFix this article or remove it please PinmemberJohn Cardinal7-Apr-07 7:04 
GeneralRe: Fix this article or remove it please PinmemberRichard Smith24-Feb-09 7:44 
GeneralDOES NOT WORK [modified] Pinmemberkogir219-Jul-06 23:10 
GeneralRe: DOES NOT WORK PinmemberRichard Smith20-Jul-06 9:11 
GeneralRe: DOES NOT WORK Pinmemberkogir220-Jul-06 16:30 
GeneralRe: DOES NOT WORK Pinmembermgrogan17-Oct-06 10:53 
GeneralGood... But PinmemberDaniel@SA12-Apr-06 15:39 
AnswerRe: Good... But PinmemberRichard Smith12-Apr-06 15:46 
GeneralRe: Good... But PinmemberDaniel@SA15-Apr-06 17:57 
GeneralInvalid Email Addresses Pinmembersumisusutefuen@hotmail.com10-Aug-05 22:36 
GeneralRe: Invalid Email Addresses Pinmemberaglt29-Sep-05 1:45 
AnswerRe: Invalid Email Addresses PinmemberRichard Smith12-Apr-06 15:51 
GeneralCompilation Optimization PinmemberNaza Coderoff27-Jul-05 9:00 
GeneralRe: Compilation Optimization PinmemberRichard Smith12-Apr-06 15:55 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150129.1 | Last Updated 11 Oct 2002
Article Copyright 2002 by Richard Smith
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid