How to parse and normalize an RFC 5322-compliant email address into its components ("John Smith", john.smith+tag, (comment), domain)?

StackOverflow https://stackoverflow.com/questions/18733240

  •  28-06-2022
  •  | 
  •  

Question

Got the validation part covered.

Just looking to parse emails into their components.

Assuming an email is valid...

Can I just look backward for the first "@" and everything after that is the domain?

And then look backward for a space and everything after that is the email address minus the quoted name?

Was it helpful?

Solution

Why parse it yourself (and risk getting it wrong), when the work has already been done?

The Mime4j library (http://james.apache.org/mime4j/) includes (among many other things related to processing email) an AddressBuilder class that has methods for parsing email addresses (including groups and individual mailboxes, as well as address lists as they may appear in an email header) out into objects that represent them (instances of the Address class and its subclasses, Group and Mailbox). The returned objects have methods that provide access to the individual components of each address (local part, domain, human-readable name, etc.).

The Javadoc for the AddressBuilder class can be found here: http://james.apache.org/mime4j/apidocs/org/apache/james/mime4j/field/address/AddressBuilder.html .

OTHER TIPS

Yes, if the address is RFC 822 compliant, there will be exactly one @ sign, and you can split on it to determine the local-part and the domain. See RFC 2822, section 3.4.1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top