Tim TrottTim TrottDo or do not, there is no try

Best Way to Validate Email Addresses in C#

By , Friday 6th July 2012 in C#

It used to be that a RegEx was the best method for 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 validate 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 you 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)
{
    try
    {
        MailAddress m = new MailAddress(emailaddress);
        return true;
    }
    catch (FormatException)
    {
        return false;
    }
}

My website and its content are free to use without the clutter of adverts, tracking cookies, 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.

About the Author

Tim Trott

Tim is a professional software engineer, designer, photographer and astronomer from the United Kingdom. You can follow him on Twitter to get the latest updates.

Further Reading
Leave a Reply

Your email address will not be published.