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 TrottC# ASP.Net MVC • July 6, 2012
Several Easy Ways to Validate Email Addresses in C#

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

As the landscape of email addresses in C# evolves and new Top Level Domains (TLDs) are created, the traditional method of using regular expressions for validation is becoming more challenging. In this guide, we'll explore how Microsoft can simplify the process of email validation in C#, allowing you to focus on other aspects of your development.

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.

Adding a reference to the Microsoft.Web.Mvc.DataAnnotations.dll assembly and the System.ComponentModel.DataAnnotations.dll assembly is a straightforward process. Simply navigate to 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;
    }
}

About the Author

Tim Trott is a senior software engineer with over 20 years of experience in designing, building, and maintaining software systems across a range of industries. Passionate about clean code, scalable architecture, and continuous learning, he specialises in creating robust solutions that solve real-world problems. He is currently based in Edinburgh, where he develops innovative software and collaborates with teams around the globe.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. 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 comments. Why not join the discussion!

New comments for this post are currently closed.