Validating email addresses in .NET - Handy Tip

If you've ever written any code that sends emails before, I'm pretty sure that you've had to validate the email addresses that are entered by users. Email address validation can be tricky at the best of times and with international domain names in email addresses, almost anything is possible. In my opinion, there is no 100% reliable way of validating email addresses, you just have to make sure that it conforms to as reasonable a standard as possible.

Validating email addresses in .NET

Using client side validation, such as the HTML5 email input type is a great way of detecting invalid email addresses. However, there may be times when you need to validate on the server. When validating email addresses in .NET, I have always previously used some pretty gnarly Regex expressions. Some of them worked, some of them didn't work that well. I recently discovered a little trick that you can use to validate email addresses, and best of all - it uses a built-in .NET class.

The System.Net.Mail namespace has a built-in class called MailAddress. If this method is passed an invalid email address, it will simply throw an error which you can use to your advantage.

In the code above, I have created a simple method that passes an email address to the MailAddress class. If the email address is invalid, the MailAddress class will throw an error which we can catch and return as a false.

The best thing about this class is that it conforms to the RFC5322 specification for email addresses, so you are able to take advantage of this without writing loads of code. While this validation isn't perfect and some dodgy emails will still slip through, it's a pretty good start.








Comments

Stephan Eberle - 2/27/2014
Hi there, a good one. But keep in mind that throwing exceptions is a costly thing and can cause a thread to run slow and sluggish. I wouldn't recommend this approach with bulk mail operations for example. Regards, Stevie

Stephan Eberle - 2/27/2014
Sorry, to be polite I should recommend something better. I forgot. So try to use regular expressions. Compared to throwing exceptions it's much faster. Regards, Stevie

Dean Hume - 2/27/2014
Hi Stephan - Thank you for reading and thanks for the comment. There have been many discussions about throwing vs. not throwing exceptions. Personally I sit on the throw fence - but for some further reading, check out this link - http://stackoverflow.com/questions/161942/how-slow-are-net-exceptions

Phil Green - 2/27/2014
It's pointless using regex. All you can possibly do is verify that @ isn't the first character in the string (indexOf). Everything else is just trying to reduce the massive possible set to a arbitrary group which is bound to fail. The only way to verify an address is to try sending a mail to it.

Stephan Eberle - 2/27/2014
Thank you for the link. Also keep in mind that the MailAddress instanciation in itself is time- and resource consuming so avoiding this when there's no need is also beneficiary. Again: To use your approach for example when you enter a mail address in a master data form -> Okay! :)

Stephan Eberle - 2/27/2014
[continued] ... Just don't use it for mass mailings. ;) Regards, Stevie

Stephan Eberle - 2/27/2014
[continued] ... Just don't use it for mass mailings. ;) Regards, Stevie

Stephan Eberle - 2/27/2014
@Phil: Sure, but that's not the point of the code above. It just checks a string against the usual ruleset of a mail address. Nothing more. And tbh I think regex are MADE to check strings against patterns. Regards, Stevie

Johannes Egger - 2/28/2014
@Stephan For bulk parsing and avoiding regex I propose to use a `System.Net.Mail.MailAddressCollection`.

Jordan - 10/14/2015
Nice and simple! I wasn't interested in reinventing the wheel and writing lots of code to validate an email address. Cheers for sharing

Jason Giles - 1/4/2016
Simple and effective solution but I have some question about the exception. What would be a better anticipated exception if an email address is invalid? This seems to just throw a generic exception and I would like to return to a user the reason the email was invalid not just a true or false? Thanks!


Add your comment

300 Characters left


Please fill this in to confirm that you are human