Reputation: 48287
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?
Upvotes: 1
Views: 3262
Reputation: 742
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 .
Upvotes: 4
Reputation: 77226
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.
Upvotes: -1