943,956 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 4600
  • VB.NET RSS
Jul 21st, 2009
0

How to check if email address is valid

Expand 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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ChrisPaul is offline Offline
8 posts
since Dec 2008
Jul 21st, 2009
0

Re: How to check if email address is valid

Click to Expand / Collapse  Quote originally posted by ChrisPaul ...
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.
VB.NET Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 9
Junior Poster
VIeditorlover is offline Offline
137 posts
since Dec 2007
Jul 21st, 2009
0

Re: How to check if email address is valid

VB.NET Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jul 22nd, 2009
0

Re: How to check if email address is valid

Thanks that works!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ChrisPaul is offline Offline
8 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Visual Basic Code, I need Help!!!
Next Thread in VB.NET Forum Timeline: VB.Net or VB 6.0





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC