Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check that email address is valid for System.Net.Mail.MailAddress

Currently, to avoid errors from being thrown up due to invalid email addresses, I do the following:

Dim mailAddress As MailAddress Try    mailAddress = New MailAddress("testing@[email protected]") Catch ex As Exception    'Invalid email End Try 

However, rather than depending on Try..Catch, is there a way of validating that the email address will be 100% valid for the MailAddress type?

I know there a plenty of regex functions out there for validating emails, but I'm looking for the function which the MailAddress type uses to validate its addresses.

like image 656
Curtis Avatar asked Aug 11 '11 14:08

Curtis


People also ask

What is System Net mail MailAddress?

The MailAddress class is used by the SmtpClient and MailMessage classes to store address information for email messages. A mail address is composed of a User name, Host name and optionally, a DisplayName. The DisplayName can contain non-ASCII characters if you encode them.

How do I check if a string is valid in email format?

To verify that the email address is valid, the IsValidEmail method calls the Regex. Replace(String, String, MatchEvaluator) method with the (@)(. +)$ regular expression pattern to separate the domain name from the email address.


1 Answers

Unfortunately, there is no MailAddress.TryParse method.

Your code is the ideal way to validate email addresses in .Net.

like image 153
SLaks Avatar answered Oct 21 '22 08:10

SLaks