I needed to validate customer email addresses. Many articles suggested to use different regEx , but they are not the same and not easy to identify, which is the best. So i decided to use MailAddress, which throw exception if MS implemented validation failed.
It world be good if the class would provide Validate or TryParse method to avoid costly exception.
/// <summary>
///
//// Validating E-mail address
/// </summary>
public static class MailHelper
{
//TODO: use Reflector or find some code to validate address as strict as ParseValue , for now just catch Exception
public static string ValidateEmailFormat(string email)
{
try
{
MailAddress from = new MailAddress(email);
}
catch (Exception exc)
{
string sMsg = exc.Message;
return sMsg;
}
return "";
}
}
Also I found a few articles about validating E-mail Against the Mail server. I wish to implement this in a future.
http://www.coveryourasp.com/ValidateEmail.asp -Short and clear explanation of approach, JSCRIPT ASP
http://www.15seconds.com/issue/030203.htm Validating E-mail Against the Mail server VB.Net
http://www.codeproject.com/KB/validation/Valid_Email_Addresses.aspx C#,reference to Peter Bromberg’s C# SMTP Mail without SMTP Service or CDO