Parse RFC 2822 email addresses in Java -


as many people unaware, email addresses require library parse. simple regexes, @(.*), not sufficient. email addresses can contain comments, can contain characters @, breaking simple regexes.

there node.js library 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 direct port of perl module mail::address.

this expect exist in java's internetaddress class, doesn't break things down further full address, can include e.g. user@gmail.com. i'm trying extract gmail.com part, doesn't include method do.

i'm surprised can't find common library solves this, presumably many people have problem. how can solved using library or no?

if need domain part email address (be aware of mailing groups since not have @) can this:

int index = "user@domain.com".lastindexof("@"); string domain = "user@domain.com".substring(index+1); 

i used lastindexof here since rfc2822 email address might contain more 1 @ symbols (if escaped). if want skip mailing groups there method in internetaddress class isgroup()

ps address contains routing information:

@donald.mit.edu,@mail.mit.edu:peter@hotmail.com 

or address literals:

peter@[192.168.134.1] 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -