Recursive AI

Parse an email address in C#

OneContact is a free, open-source contact data library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies, no network access.

Parse returns the display name apart from the address.

Terminal
dotnet add package RecursiveAI.OneContact

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneContact;
var email = EmailAddress.Parse(
    "Jane Doe <Jane@EXAMPLE.COM>");
Console.WriteLine(email.DisplayName);
Console.WriteLine(email.LocalPart);
Console.WriteLine(email.Domain);
Console.WriteLine(email.Canonical);

Prints the name, local part, domain, and normalized address, in that order:

Text
Jane Doe
Jane
example.com
Jane@example.com

Unzip the example, then dotnet run.

Full program

The program uses two inline strings: a quoted name that contains a comma, and an address with no name.

C#
using RecursiveAI.OneContact;
 
foreach (var text in new[]
{
    "\"Doe, Jane\" <Jane+news@EXAMPLE.COM>",
    "jane@example.com"
})
{
    var email = EmailAddress.Parse(text);
    Console.WriteLine($"Name: {email.DisplayName ?? "(none)"}");
    Console.WriteLine($"Local part: {email.LocalPart}");
    Console.WriteLine($"Domain: {email.Domain}");
    Console.WriteLine($"Address: {email.Canonical}");
}

dotnet run prints:

Text
Name: Doe, Jane
Local part: Jane+news
Domain: example.com
Address: Jane+news@example.com
Name: (none)
Local part: jane
Domain: example.com
Address: jane@example.com

Parse throws ContactParseException if it can’t read the input as one mailbox; TryParse returns false instead.

Variations

Read a mailbox whose quoted local part itself contains an @:

C#
using RecursiveAI.OneContact;
var email = EmailAddress.Parse(
    "\"orders@west\"@example.com");
Console.WriteLine(email.LocalPart);
Console.WriteLine(email.Domain);
Text
"orders@west"
example.com

The short answer

Call EmailAddress.Parse on one mailbox string. DisplayName is the optional name, LocalPart and Domain hold the two parts of the address, and Canonical gives you the address without the display name. Use the canonical address for an address field and keep the name in a separate field, rather than trying to recover it later from the canonical value.

How do I parse an email address in C#?

Pass the whole field value, including any quotes and angle brackets. In Jane Doe <Jane@EXAMPLE.COM>, the name is outside the brackets and the address is inside. When the field is just jane@example.com, DisplayName is null; the parser doesn’t guess a name from jane. The full program shows both forms and demonstrates that the comma in "Doe, Jane" belongs to the name. The quotes are syntax and are not part of the returned name.

This is a parser for one mailbox, not an email-message parser. If you start with a full message, first get the mailbox field value from your message reader. If the field contains an address list or a group, don’t pass the whole field to EmailAddress.Parse or split it at every comma. The email grammar notes explain what the parser accepts and why a list isn’t one mailbox.

How do I split an email address into its local part and domain?

Read LocalPart and Domain from the result. Splitting at the first @ fails on the quoted local part in the variation, and splitting the whole input ignores the fact that a display name can be present. Parsing first finds the actual address boundary and keeps meaningful quotes on the local part. That’s different from quotes around a display name, which are removed.

The parser keeps the local part’s case and a suffix such as +news; the domain is lowercased. A domain with internationalized labels is stored in ASCII form in Domain, and UnicodeDomain gives the readable form. If you need the original field exactly as received, keep the input separately. Canonical gives you a normalized address, not a round-trip copy of the field: the display name and comments aren’t included. These transformations are described in the canonical form notes.

All onecontact recipes