Monday, 9 December 2013

Validate Email Address in C#

Description:

In this article, I am going to write C# code examples to Check or Validate Email Address/Email ID using Regular Expressions and C# code example to Test EMail Address is valid or not by sending Test mail.

Summary:


Validate Email Address in C# using Regular Expressions

Here, we are using Regex class to represents Regular Expressions
public static bool IsValidEmailAddressByRegex(string mailAddress)
{
    Regex mailIDPattern = new Regex(@"[\w-]+@([\w-]+\.)+[\w-]+");

    if (!string.IsNullOrEmpty(mailAddress) && mailIDPattern.IsMatch(mailAddress))
    {
        return true;
    }
    else
    {
        return false;
    }
}

Validate Email Address/Email ID in C# using Regular Expressions(2)

public static bool IsValidEmailAddressByRegex2(string mailAddress)
{
    Regex mailIDPattern = new Regex(@"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"
    + @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)"
    + @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$", RegexOptions.IgnoreCase);

    if (!string.IsNullOrEmpty(mailAddress) && mailIDPattern.IsMatch(mailAddress))
    {
        return true;
    }
    else
    {
        return false;
    }
}

Validate Email Address in C# using Regular Expressions(3)

public static bool IsValidEmailAddressByRegex3(string mailAddress)
{
    Regex mailIDPattern = new Regex("^((([a-z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])+(\\.([a-z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])+)*)|((\\x22)((((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(([\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(\\\\([\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF]))))*(((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(\\x22)))@((([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])*([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])))\\.)+(([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])*([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])))\\.?$", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.Compiled);

    if (!string.IsNullOrEmpty(mailAddress) && mailIDPattern.IsMatch(mailAddress))
    {
        return true;
    }
    else
    {
        return false;
    }
}

Validate Email Address Pattern and Test Email ID is Valid or Not in C#

You can use this C# function to validate E Mail Address string pattern and to Test the given E Mail ID is Valid Address or not by sending the Test mail.
public static bool ValidateAndTestEmailAddress(string mailAddress)
{
    Regex mailIDPattern = new Regex(@"[\w-]+@([\w-]+\.)+[\w-]+");

    if (!string.IsNullOrEmpty(mailAddress) && mailIDPattern.IsMatch(mailAddress))
    {
        System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();

        // your mail server address
        smtpClient.Host = "smtp.server1.net";

        // your mail server port address
        smtpClient.Port = 25;

        // Credentials to use mail server if required
        smtpClient.Credentials = new NetworkCredential("admin@morgantechspac.com", "yourpassword");

        try
        {
            // Send Test Mail to Check Email Address/E mail ID is Valid or Not ...

            smtpClient.Send("admin@morgantechspac.com", mailAddress, "Mail ID testing", "Validate Mail Address");

            //  Email Address was Verified!

            return true;
        }
        catch (Exception exp)
        {
            //Failed to send test mail...
            Console.WriteLine(exp.Message);
            return false;
        }
    }
    else
    {
        //Invalid email address pattern...
        return false;
    }
}

Validate Email Address Pattern and Check Is Valid EMail Domain

You can use this Regex pattern to validate Email Address string pattern and also to check whether the given EMail Address contains Valid Email Domain or Not.

From http://www.regular-expressions.info/email.html
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@
(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|
biz|info|mobi|name|aero|asia|jobs|museum)\b

Thanks,
Morgan
Software Developer

Advertisements
Advertisements

1 comment:

  1. string uname = Convert.ToString(dto.Rows[0]["USER_NAME"]);
    String expression = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
    Regex regex = new Regex(expression);
    Match m = regex.Match(uname);
    if (m.Success)
    {
    emailId = uname;

    }

    ReplyDelete