Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse RFC 2822 email addresses in Java

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?

like image 265
djechlin Avatar asked Aug 28 '13 20:08

djechlin


1 Answers

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]
like image 185
Grigory Avatar answered Oct 07 '22 22:10

Grigory