Record Class EmailAddress
java.lang.Object
java.lang.Record
com.google.common.labs.email.EmailAddress
public record EmailAddress(Optional<String> displayName, String localPart, String domain)
extends Record
Represents an email address according to RFC 5322, designed as a modern,
type-safe replacement for Comparison with
javax.mail.InternetAddress.
For example:
EmailAddress emailAddress = EmailAddress.parse("john-doe@gmail.com");
RFC 5322 Compliance Profile
- Address Specification (addr-spec): Supports the standard
local-part@domainformat (RFC 5322 §3.4.1). - Name-Addr: Fully supports
"display-name" <addr-spec>syntax (RFC 5322 §3.4). - Quoted-Strings: Complies with RFC 5322 §3.2.4, supporting backslash-escaped characters within double-quoted display names.
- Phrases (unquoted names): Supports RFC 5322 "atoms" in
display names, forbidding "specials", i.e. the
()<>[]:;@\,"characters, while allowing periods for real-world usability (e.g., "J.R.R. Tolkien"). - Folding White Space (FWS): Supports optional whitespace between the display name and the angle-bracketed address.
Comparison with javax.mail.InternetAddress
| Feature | InternetAddress | EmailAddress |
|---|---|---|
| Immutability | Mutable pojo | Immutable record |
| DNS labels | Permissive (allows illegal hyphens) | Strict (RFC 1035/1123 63-char limit) |
| Validation | Lazy – need to call validate() | Eager – parse() throws if invalid |
| I18n | Often requires Encoded-Words | Native BMP support |
Intentionally Omitted Legacy Features
To maintain compatibility with modern MTAs (Gmail, Outlook) and mitigate header injection risks, the following RFC 5322 edge cases are excluded:
- Quoted Local-Parts: (e.g.,
"john doe"@domain) - Deprecated in practice. - Comments (CFWS): (e.g.,
name(comment) <addr>) - De facto obsolete. - Domain Literals: (e.g.,
user@[192.168.1.1]) - IP routing is rarely supported. - Obsolete Syntax: (RFC 5322 §4) - Legacy syntax like "quoted-pairs" in unquoted names.
- Since:
- 9.9.4
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Parser<EmailAddress> The parser for email address, according to RFC 5322, and supporting BMP characters. -
Constructor Summary
ConstructorsConstructorDescriptionEmailAddress(Optional<String> displayName, String localPart, String domain) Prefer using theof(java.lang.String, java.lang.String)factory method. -
Method Summary
Modifier and TypeMethodDescriptionaddress()Returns theaddr-spec, in the form ofuser@mycompany.com.Returns the value of thedisplayNamerecord component.domain()Returns the value of thedomainrecord component.final booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.Returns the value of thelocalPartrecord component.static EmailAddressFor example:EmailAddress.of("user", "mycompany.com").static EmailAddressParsesaddressand throwsParser.ParseExceptionif failed.static List<EmailAddress> parseAddressList(String addressList) ParsesaddressListaccording to RFC 5322 and returns an immutable list ofEmailAddress.toString()Returns the full email address, in the form oflocal-part@domainor"display name" <local-part@domain>.withDisplayName(String displayName) Returns an otherwise equivalentEmailAddressbut withdisplayName.
-
Field Details
-
PARSER
The parser for email address, according to RFC 5322, and supporting BMP characters.Prefer using the
parse(java.lang.String)convenience method. This constant is to be used for composition, for example to parse a list of email addresses:EmailAddress.PARSER.skipping(whitespace()).parseToStream(emailAddresses).toList();
-
-
Constructor Details
-
EmailAddress
Prefer using theof(java.lang.String, java.lang.String)factory method. You can callwithDisplayName(java.lang.String)to optionally attach a display name.
-
-
Method Details
-
withDisplayName
Returns an otherwise equivalentEmailAddressbut withdisplayName. -
of
For example:EmailAddress.of("user", "mycompany.com"). -
address
Returns theaddr-spec, in the form ofuser@mycompany.com. -
toString
-
parse
Parsesaddressand throwsParser.ParseExceptionif failed. -
parseAddressList
ParsesaddressListaccording to RFC 5322 and returns an immutable list ofEmailAddress.Both colon (
,) and semicolon (;) are supported as delimiters, with whitespaces ignored. Trailing delimiters are allowed.Empty input will result in an empty list being returned.
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared withObjects::equals(Object,Object). -
displayName
-
localPart
Returns the value of thelocalPartrecord component.- Returns:
- the value of the
localPartrecord component
-
domain
Returns the value of thedomainrecord component.- Returns:
- the value of the
domainrecord component
-