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.
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.
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.
Unfortunately, there is no MailAddress.TryParse
method.
Your code is the ideal way to validate email addresses in .Net.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With