First off, the email address you provided is in a valid format. -- You can take it a step further and verify if the domain is valid or not as well, if you like; but either way, you should be validating ownership, in which case, you will know that the email address and domain are valid.
Secondly, you should really not be using Regex to validate an email address; email address validation is built-in to the .NET framework, and you shouldn't be recreating the wheel for something like this.
A simple validation function that performs both checks, looks like this:
public static bool IsValidEmailAddress(string emailAddress, bool verifyDomain = true)
{
var result = false;
if (!string.IsNullOrWhiteSpace(emailAddress))
{
try
{
// Check Format (Offline).
var addy = new MailAddress(emailAddress);
if (verifyDomain)
{
// Check that a valid MX record exists (Online).
result = new DnsStubResolver().Resolve<MxRecord>(addy.Host, RecordType.Mx).Any();
}
else
{
result = true;
}
}
catch (SocketException)
{
result = false;
}
catch (FormatException)
{
result = false;
}
}
return result;
}
To run the code, you will need to install the NuGet package ARSoft.Tools.Net
, which is required for MX record lookup*, and you will need to add the appropriate using declarations for the various classes used (should be pretty automatic in VisualStudio these days).
(*: Simply checking for a valid host name via System.Net.Dns.GetHost*
is not enough, as that can give you some false negatives for some domains which only have MX entries, such as admin@fubar.onmicrosoft.com
, etc.)