Several Easy Ways to Validate Email Addresses in C#

Instead of using a regex to validate email addresses, there are built in tools which can perform the task just as well, if not better.

By Tim Trott | C# ASP.Net MVC | July 6, 2012

Instead of using a regular expression to validate an email address, you can use the built-in functions of the MailAddress class or Data Annotations.

It used to be that regular expressions were the best method to validate 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#?

Validate Email Addresses with Data Annotations

Data Annotation Model Binder performs validation within an ASP.NET MVC application, although you can reference the assembly and namespace in any project type.

Add a reference to the Microsoft.Web.Mvc.DataAnnotations.dll assembly and the System.ComponentModel.DataAnnotations.dll assembly from within the Project options > Add Reference dialogue.

Next, add the namespace and the code to validate an email address.

C#
using System.ComponentModel.DataAnnotations

public static bool IsEmailValid(string emailaddress)
{
  return new EmailAddressAttribute().IsValid(emailaddress);
}

Validate Email Addresses using Regular Expressions

Regular Expressions, or Regex, are another popular method for validating mail addresses in your C# code.

C#
public static bool IsEmailValid(string emailaddress)
{
  / returns true if the input is a valid email
  return Regex.IsMatch(emailaddress, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
}

Validate Email Addresses with MailAddress

The .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 

C#
public bool IsEmailValid(string emailaddress)
{
    try
    {
        MailAddress m = new MailAddress(emailaddress);
        return true;
    }
    catch (FormatException)
    {
        return false;
    }
}
Was this article helpful to you?
 

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

This post has 6 comment(s). Why not join the discussion!

We respect your privacy, and will not make your email public. Learn how your comment data is processed.

  1. SR

    On Friday 15th of January 2021, Scott Reed said

    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!

  2. JD

    On Thursday 11th of June 2020, John DeSpirito said

    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.

  3. VL

    On Monday 27th of April 2020, Vikas Lalwani said

    C#
    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#

  4. DB

    On Thursday 23rd of May 2019, David Barrows said

    Both methods consider the following email address valid:

    someperson@somedomain

    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.

  5. NB

    On Wednesday 17th of April 2019, Norm Bell said

    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.

    1. VI

      On Friday 25th of September 2020, Vikas replied

      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