Hello,

I have a question. How can i determine if an email address is in the correct format. I use the System.Net.Mail.MailMessage to send my messages. I am getting my To email addresses from the database but when the address is not in the correct email format the program stops sending. How can i determine if its in correct format and if it is not then jump to the next.? Thanks.

Hello,

I have a question. How can i determine if an email address is in the correct format. I use the System.Net.Mail.MailMessage to send my messages. I am getting my To email addresses from the database but when the address is not in the correct email format the program stops sending. How can i determine if its in correct format and if it is not then jump to the next.? Thanks.

public static bool IsValidEmailAddress(string sEmail)
{
  if (sEmail == null)
    return false;
 else
    return Regex.IsMatch(sEmail, @"
^[-a-zA-Z0-9][-.a-zA-Z0-9]*@[-.a-zA-Z0-9]+(\.[-.a-zA-Z0-9]+)*\.(com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|[a-zA-Z]{2})$",RegexOptions.IgnorePatternWhitespace);

}
public static bool IsValidEmailAddress(string sEmail)
{
  if (sEmail == null)
    return false;
 else
    return Regex.IsMatch(sEmail, @"
^[-a-zA-Z0-9][-.a-zA-Z0-9]*@[-.a-zA-Z0-9]+(\.[-.a-zA-Z0-9]+)*\.(com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|[a-zA-Z]{2})$",RegexOptions.IgnorePatternWhitespace);

}

That solution will not always work. Your best bet is to try to create a New MailAddress() and catch the exception. If it throws an exception then it is invalid, otherwise assume it is valid.

This question has already been answered. Start a new discussion instead.