Learning never exhausts the mind
Home >  Coding > C# ASP.Net MVC > Best Way to Validate Email Addresses in C#

Published 6th July 2012 by

It used to be that a RegEx was the best method for validating email addresses in C#, but with all the new Top Level Domains (TLD's) flying around and new ones being released, this is becoming an increasingly difficult task.

So instead, why not let Microsoft do the hard work of validating email addresses in C#? Their .Net library gets updated regularly with all the new validation methods automatically across all platforms (forms, asp.net etc..) meaning you need only call their methods from your code and you are up to date.

Instead of using a regular expression to validate an email address, you can use the System.Net.Mail.MailAddress class. To determine whether an email address is valid, pass the email address to the MailAddress.MailAddress(String) class constructor.

Source: Microsoft ASP.Net Website

public bool IsEmailValid(string emailaddress)
        MailAddress m = new MailAddress(emailaddress);
        return true;
    catch (FormatException)
        return false;
6 thoughts on “Best Way to Validate Email Addresses in C#
  • 15th January 2021 at 10:37 am

    I just picked up one of my developers for using this technique as an anti pattern. You should definitely not be using Try Catches for functionality, they are supposed to be use to wrap code for unexpected errors and are expensive. Very bad practice!

  • 11th June 2020 at 4:05 pm

    Careful, this function considered the following string listing multiple addresses to be a valid e-mail address:

    "address1@domain.com, address2@domain.com"

    The object it created thinks that the first address is simply it's display name.

  • 27th April 2020 at 11:45 am

    string pattern = @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z";

    //check first string
    if (Regex.IsMatch(EmailId1 , pattern))
    //if email is valid
    Console.WriteLine(EmailId1+ " is a valid Email address ");

    Source: email validation c#

  • David Barrows
    23rd May 2019 at 9:54 am

    Both methods consider the following email address valid:


    Some would argue that's a valid email address, but for most intents and purposes, it's not valid. However both the MailAddress class and the System.ComponentModel.DataAnnotations attribute approach consider that valid.

  • 17th April 2019 at 8:48 pm

    Another way to validate an email address is to use the EmailAddressAttribute class's IsValid method:

    var isValid = new EmailAddressAttribute().IsValid(EmailAddress);

    This class is in the System.ComponentModel.DataAnnotations namespace.

    • 25th September 2020 at 8:45 pm

      var isValid = new EmailAddressAttribute().IsValid(EmailAddress); does not validate abc@ddd. This is a valid email for EmailAddressAttribute().IsValid() even though its missing ".com", ".io" etc part


Leave a Reply

Fields marked with * are mandatory.

We respect your privacy, and will not make your email public. Hashed email address may be checked against Gravatar service to retrieve avatars. This site uses Akismet to reduce spam. Learn how your comment data is processed.