Parse RFC 2822 email addresses in Java

不打扰是莪最后的温柔 提交于 2019-12-10 15:57:29

问题


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?


回答1:


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]



回答2:


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.



来源:https://stackoverflow.com/questions/18498036/parse-rfc-2822-email-addresses-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!