954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to check if email address is valid

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.

ChrisPaul
Newbie Poster
8 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

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);

}
VIeditorlover
Junior Poster
137 posts since Dec 2007
Reputation Points: 10
Solved Threads: 9
 
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.

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

Thanks that works!

ChrisPaul
Newbie Poster
8 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You