Validate EMail in C#

To validate an email address in C#, you can use a regular expression. Here's an example:

using System.Text.RegularExpressions;

public static bool IsValidEmail(string email)
{
    if (string.IsNullOrWhiteSpace(email))
        return false;

    try
    {
        // Normalize the domain
        email = Regex.Replace(email, @"(@)(.+)$", DomainMapper,
            RegexOptions.None, TimeSpan.FromMilliseconds(200));

        // Examines the domain part of the email and normalizes it.
        static string DomainMapper(Match match)
        {
            // Use IdnMapping class to convert Unicode domain names.
            var idn = new IdnMapping();

            // Pull out and process domain name (throws ArgumentException on invalid)
            var domainName = idn.GetAscii(match.Groups[2].Value);

            return match.Groups[1].Value + domainName;
        }
    }
    catch (RegexMatchTimeoutException)
    {
        return false;
    }
    catch (ArgumentException)
    {
        return false;
    }

    try
    {
        return Regex.IsMatch(email,
            @"^[^@\s]+@[^@\s]+\.[^@\s]+$",
            RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250));
    }
    catch (RegexMatchTimeoutException)
    {
        return false;
    }
}

This method will return true if the email address is valid, and false otherwise.

To hide some characters of an email address in C#, you can replace some characters with asterisks using a regular expression. Here's an example:

string email = "john.doe@example.com";
string maskedEmail = Regex.Replace(email, @"(?<=.).(?=[^@]*?@)", "*", RegexOptions.Compiled);

In this example, the regular expression (?<=.).(?=[^@]*?@) matches any character that is not the first character before the @ symbol, and replaces it with an asterisk. The resulting string maskedEmail will be j***.d**@example.com.

  1. C# validate email address regex: Validating an email address using a regular expression in C#.

    using System.Text.RegularExpressions;
    
    string emailAddress = "example@example.com";
    bool isValid = Regex.IsMatch(emailAddress, @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$");
    
  2. Email address validation using Regular Expressions in C#: Implementing email address validation using regular expressions in C#.

    using System.Text.RegularExpressions;
    
    string emailAddress = "example@example.com";
    bool isValid = Regex.IsMatch(emailAddress, @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$");
    
  3. Check if email is valid in C#: Checking if an email address is valid using a regular expression.

    using System.Text.RegularExpressions;
    
    bool IsValidEmail(string email)
    {
        return Regex.IsMatch(email, @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$");
    }
    
  4. C# validate email address format: Validating the format of an email address using a regular expression.

    using System.Text.RegularExpressions;
    
    string emailAddress = "example@example.com";
    bool isValidFormat = Regex.IsMatch(emailAddress, @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$");
    
  5. Regular expression for email validation in C#: Defining a regular expression for email validation in C#.

    using System.Text.RegularExpressions;
    
    string emailRegexPattern = @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$";
    Regex emailRegex = new Regex(emailRegexPattern);
    
    string emailAddress = "example@example.com";
    bool isValid = emailRegex.IsMatch(emailAddress);
    
  6. Using System.Net.Mail to validate email in C#: Using System.Net.Mail classes to validate an email address in C#.

    using System.Net.Mail;
    
    string emailAddress = "example@example.com";
    try
    {
        MailAddress mailAddress = new MailAddress(emailAddress);
        bool isValid = true; // Email address is valid
    }
    catch (FormatException)
    {
        bool isValid = false; // Email address is not valid
    }
    
  7. Validate email address using MailAddress class in C#: Utilizing the MailAddress class in System.Net.Mail for email validation in C#.

    using System.Net.Mail;
    
    string emailAddress = "example@example.com";
    try
    {
        MailAddress mailAddress = new MailAddress(emailAddress);
        bool isValid = true; // Email address is valid
    }
    catch (FormatException)
    {
        bool isValid = false; // Email address is not valid
    }
    
  8. C# check if email is well-formed: Checking if an email address is well-formed using a try-catch block with MailAddress class.

    using System.Net.Mail;
    
    bool IsWellFormedEmail(string email)
    {
        try
        {
            new MailAddress(email);
            return true;
        }
        catch (FormatException)
        {
            return false;
        }
    }
    
  9. Validate email against domain whitelist in C#: Validating an email address against a domain whitelist using a regular expression.

    using System.Text.RegularExpressions;
    
    string emailAddress = "example@example.com";
    bool isValid = Regex.IsMatch(emailAddress, @"^[a-zA-Z0-9._%+-]+@(example\.com|whitelisted\.com)$");