All site contents ©2017 Casey Connor unless otherwise noted.

EmailAddress.java

Last Updated: Wednesday, 17 September 2014

The world's only more-or-less-2822-compliant Java-based email address extractor/verifier, with some useful header verification as well.

This code has accurately and efficiently parsed upwards of 10 billion email addresses at boxbe.com.

Background: Reasonably, Javamail's InternetAddress class does not have a very robust parser, and often "fails" silently (technically not a failure, but not suitable for those that need to extract addresses accurately and predictably). This class can reliably extract the pertinent information from any well-formed address, as well as perform several other useful functions relating to addresses and email headers.

Currently, this project is hosted at Boxbe. You can download it there. See the code for extensive documentation.

This is reasonably fast code, but it does make extensive use of regular expressions. It would be much faster if implemented with a lexer. Feel free.

This class is designed to integrate with Javamail (this class will require that you have a javamail mail.jar in your classpath), but you could easily change the existing methods around to not use Javamail at all.

Examples of passing (2822-valid) addresses, believe it or not (spaces and all):

bob @example.com

"bob" @ example.com

bob (comment) (other comment) @example.com (personal name)

"<bob \" (here) " < (hi there) "bob(the man)smith" (hi) @ (there) example.com (hello) > (again)

None of the above are permitted by javamail's InternetAddress parsing.

By using EmailAddress.getInternetAddress(), you can retrieve an InternetAddress object that, when toString()'ed, would reveal that the parser had converted the above examples into:

<bob@example.com>

<bob@example.com>

"personal name" <bob@example.com>

"<bob \" (here)" <"bob(the man)smith"@example.com>