17 using System.Globalization;
18 using System.Text.RegularExpressions;
38 if (
string.IsNullOrWhiteSpace(emailAddress))
43 emailAddress = NormalizeEmailAddressDomainName(emailAddress);
44 if (emailAddress ==
null)
60 private static string NormalizeEmailAddressDomainName(
string emailAddress)
65 emailAddress = RegularExpression.EmailDomainName.Replace(emailAddress, match =>
68 var idn =
new IdnMapping();
71 var domainName = idn.GetAscii(match.Groups[2].Value);
73 return match.Groups[1].Value + domainName;
89 private static readonly TimeSpan MatchTimeout = TimeSpan.FromMilliseconds(100);
96 public static readonly Regex
EmailDomainName =
new Regex(
@"(@)(.+)$", RegexOptions.Compiled, MatchTimeout);
103 public static readonly Regex
Email =
new Regex(
@"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-0-9a-z]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$", RegexOptions.IgnoreCase | RegexOptions.Compiled, MatchTimeout);