Parse RFC 2822 email addresses in Java

4k views Asked by At

As many people are unaware, email addresses require a library to parse. Simple regexes, like @(.*), are not sufficient. Email addresses can even contain comments, which can contain characters like @, breaking simple regexes.

There is a Node.js library that parses RFC 2822 addresses:

var address = addresses[0];
console.log("Email address: " + address.address);
console.log("Email name: " + address.name());
console.log("Reformatted: " + address.format());
console.log("User part: " + address.user());
console.log("Host part: " + address.host());

which is an almost direct port of the perl module Mail::Address.

This is something that I would expect to exist in Java's InternetAddress class, but it doesn't break things down any further than the full address, which can include e.g. [email protected]. But I'm trying to extract the gmail.com part, which it doesn't include a method to do.

I'm surprised I can't find a common library that solves this, but presumably many people have this problem. How can this be solved using a library or no?

2

There are 2 answers

0
Grigory On

If you need to just get domain part from email address (be aware of mailing groups since they do not have @) you can do like this:

int index = "[email protected]".lastIndexOf("@");
String domain = "[email protected]".substring(index+1);

I used lastIndexOf here since by RFC2822 email address might contain more than one @ symbols (if it is escaped). If you want to skip mailing groups there is method in InternetAddress class isGroup()

PS also it could be that address contains routing information:

@donald.mit.edu,@mail.mit.edu:[email protected]

Or address literals:

peter@[192.168.134.1]
1
Bill Shannon On

Most of the time there's no need to split the address into its constituent parts, since there's nothing you can do with the parts. Assuming you have a valid need, there are libraries out there that will do a more complete validation than JavaMail does. Here's one I found quickly. I'm sure there are others.