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