I want a function to test that a string is formatted like an email address.
What comes built-in with the .NET framework to do this?
This works:
Function IsValidEmailFormat(ByVal s As String) As Boolean
Try
Dim a As New System.Net.Mail.MailAddress(s)
Catch
Return False
End Try
Return True
End Function
But, is there a more elegant way?
Don't bother with your own validation. .NET 4.0 has significantly improved validation via the MailAddress
class. Just use MailAddress address = new MailAddress(input)
and if it throws, it's not valid. If there is any possible interpretation of your input as an RFC 2822 compliant email address spec, it will parse it as such. The regexes above, even the MSDN article one, are wrong because they fail to take into account a display name, a quoted local part, a domain literal value for the domain, correct dot-atom specifications for the local part, the possibility that a mail address could be in angle brackets, multiple quoted-string values for the display name, escaped characters, unicode in the display name, comments, and maximum valid mail address length. I spent three weeks re-writing the mail address parser in .NET 4.0 for System.Net.Mail and trust me, it was way harder than just coming up with some regular expression since there are lots of edge-cases. The MailAddress
class in .NET 4.0 beta 2 will have this improved functionality.
One more thing, the only thing you can validate is the format of the mail address. You can't ever validate that an email address is actually valid for receiving email without sending an email to that address and seeing if the server accepts it for delivery. It is impossible and while there are SMTP commands you can give to the mail server to attempt to validate it, many times these will be disabled or will return incorrect results since this is a common way for spammers to find email addresses.
Thank you for responding. I'm glad to hear that this has gotten some attention from Microsoft! But, just to clarify, you're saying that the best practice is to catch an exception as I've done in the function above?
I would like to add that it is generally bad practice to catch an exception as a way of validating data. Mainly the reason is performance especialy when performing such validation over large sets of data but in generally it is just considered bad practice. For things as simple as email addresses I would opt for using a RegEx instead.
However, this seems to allow simply "part1@part2" without requiring the top-level domain of ".com" or whatnot.
A long time since I wrote this, but it shouldn't allow part2 to just be a random word, it can either be a dot atom OR a domain literal, and domain literals could just be "part2" but must be enclosed in "[" and "]" in order to be valid. A dot-atom must have a "." in it and be preceded and followed by a single legal dot-atom character. The spec is here: tools.ietf.org/html/rfc2822#page-17 Also, it's possible that it will interpret a domain literal without brackets as a domain literal and will add the brackets for you. Don't remember for sure, but sounds like something I would do.
I can also type myname@mail*.com and it accepts it, so this doesn't work :(