Check that email address is valid for System.Net.Mail.MailAddress

If you need to make sure a given email address is valid according to the IETF standards - which the MailAddress class seems to follow only partially, at the time of this writing - I suggest you to take a look at EmailVerify.NET, a .NET component you can easily integrate in your solutions. It does not depend on regular expressions to perform its job but it relies on an internal finite state machine, so it is very very fast.

  • Website
  • Online demo

Disclaimer: I am the lead developer of this component.


Recently the .NET API was extended with a MailAddress.TryCreate method, probably coming in future releases, which will eliminate the need for the common try-catch boilerplate: https://github.com/dotnet/runtime/commit/aea45f4e75d1cdbbfc60daae782d1cfeb700be02


Unfortunately, there is no MailAddress.TryParse method.

Your code is the ideal way to validate email addresses in .Net.


Not really an answer to this question per se, but in case anyone needs it, I wrote a C# function for validating email addresses using this method.

FixEmailAddress("[email protected]")

returns "[email protected]"

FixEmailAddress("wa@[email protected],[email protected],asdfdsf,[email protected]")

returns "[email protected],[email protected]"

I process lists of email addresses this way because a comma separated list of emails is a valid parameter for MailAddressCollection.Add()

/// <summary>
/// Given a single email address, return the email address if it is valid, or empty string if invalid.
/// or given a comma delimited list of email addresses, return the a comma list of valid email addresses from the original list.
/// </summary>
/// <param name="emailAddess"></param>
/// <returns>Validated email address(es)</returns>  
public static string FixEmailAddress(string emailAddress)
{

   string result = "";

    emailAddress = emailAddress.Replace(";",",");
   if (emailAddress.Contains(","))
   {
       List<string> results = new List<string>();
       string[] emailAddresses = emailAddress.Split(new char[] { ',' });
       foreach (string e in emailAddresses)
       {
           string temp = FixEmailAddress(e);
           if (temp != "")
           {
               results.Add(temp);
           }
       }
       result = string.Join(",", results);
   }
   else
   {

       try
       {
           System.Net.Mail.MailAddress email = new System.Net.Mail.MailAddress(emailAddress);
           result = email.Address;
       }
       catch (Exception)
       {
           result = "";
       }

   }

   return result;

}