Click here to Skip to main content
12,406,528 members (30,700 online)
Click here to Skip to main content
Add your own
alternative version


52 bookmarked

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.


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
    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}",

            Console.WriteLine("Quoted Name: {0}",

            Console.WriteLine("Local Part: {0}",

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

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.


  • October 11, 2002 - Initial posting


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


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 | :)

You may also be interested in...

Comments and Discussions

GeneralFix this article or remove it please Pin
John Cardinal7-Apr-07 6:04
memberJohn Cardinal7-Apr-07 6:04 
GeneralRe: Fix this article or remove it please Pin
Richard Smith24-Feb-09 6:44
memberRichard Smith24-Feb-09 6:44 
GeneralDOES NOT WORK [modified] Pin
kogir219-Jul-06 22:10
memberkogir219-Jul-06 22:10 
GeneralRe: DOES NOT WORK Pin
Richard Smith20-Jul-06 8:11
memberRichard Smith20-Jul-06 8:11 
GeneralRe: DOES NOT WORK Pin
kogir220-Jul-06 15:30
memberkogir220-Jul-06 15:30 
GeneralRe: DOES NOT WORK Pin
mgrogan17-Oct-06 9:53
membermgrogan17-Oct-06 9:53 
GeneralGood... But Pin
Daniel@SA12-Apr-06 14:39
memberDaniel@SA12-Apr-06 14:39 
AnswerRe: Good... But Pin
Richard Smith12-Apr-06 14:46
memberRichard Smith12-Apr-06 14:46 
GeneralRe: Good... But Pin
Daniel@SA15-Apr-06 16:57
memberDaniel@SA15-Apr-06 16:57 
GeneralInvalid Email Addresses Pin
sumisusutefuen@hotmail.com10-Aug-05 21:36
membersumisusutefuen@hotmail.com10-Aug-05 21:36 
GeneralRe: Invalid Email Addresses Pin
aglt29-Sep-05 0:45
memberaglt29-Sep-05 0:45 
AnswerRe: Invalid Email Addresses Pin
Richard Smith12-Apr-06 14:51
memberRichard Smith12-Apr-06 14:51 
GeneralCompilation Optimization Pin
Naza Coderoff27-Jul-05 8:00
memberNaza Coderoff27-Jul-05 8:00 
GeneralRe: Compilation Optimization Pin
Richard Smith12-Apr-06 14:55
memberRichard Smith12-Apr-06 14:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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
Web01 | 2.8.160730.1 | Last Updated 11 Oct 2002
Article Copyright 2002 by Richard Smith
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid