How to check if email address is valid

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 8
Reputation: ChrisPaul is an unknown quantity at this point 
Solved Threads: 0
ChrisPaul ChrisPaul is offline Offline
Newbie Poster

How to check if email address is valid

 
0
  #1
Jul 21st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 97
Reputation: VIeditorlover is an unknown quantity at this point 
Solved Threads: 5
VIeditorlover's Avatar
VIeditorlover VIeditorlover is offline Offline
Junior Poster in Training

Re: How to check if email address is valid

 
0
  #2
Jul 21st, 2009
Originally Posted by ChrisPaul View Post
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.
  1. public static bool IsValidEmailAddress(string sEmail)
  2. {
  3. if (sEmail == null)
  4. return false;
  5. else
  6. return Regex.IsMatch(sEmail, @"
  7. ^[-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);
  8.  
  9. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,211
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 572
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: How to check if email address is valid

 
0
  #3
Jul 21st, 2009
Originally Posted by VIeditorlover View Post
  1. public static bool IsValidEmailAddress(string sEmail)
  2. {
  3. if (sEmail == null)
  4. return false;
  5. else
  6. return Regex.IsMatch(sEmail, @"
  7. ^[-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);
  8.  
  9. }
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.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 8
Reputation: ChrisPaul is an unknown quantity at this point 
Solved Threads: 0
ChrisPaul ChrisPaul is offline Offline
Newbie Poster

Re: How to check if email address is valid

 
0
  #4
Jul 22nd, 2009
Thanks that works!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC