6

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. user@gmail.com. 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?

djechlin
  • 59,258
  • 35
  • 162
  • 290

2 Answers2

2

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 = "user@domain.com".lastIndexOf("@");
String domain = "user@domain.com".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:peter@hotmail.com

Or address literals:

peter@[192.168.134.1]
Grigory
  • 465
  • 5
  • 16
0

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.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • 1
    Well in particular I need the domain right now - trying to e.g. extract "gmail.com" from "user@gmail.com". I was really surprised I couldn't find this in Javamail or Apache stuff, but I'll look at your link. I guess that might be less useful from the standpoint of building an IMAP or SMTP client or server, but we use this stuff more for analysis of emails (categorization as personal / subscription, etc.) – djechlin Aug 28 '13 at 23:05