Record Class EmailAddress
java.lang.Object
java.lang.Record
com.google.common.labs.email.EmailAddress
- Record Components:
displayName- the"J.R.R. Tolkien"fromJ.R.R. Tolkien <tolkien@lotr.org>localPart- the"tolkien"fromJ.R.R. Tolkien <tolkien@lotr.org>domain- the"lotr.org"fromJ.R.R. Tolkien <tolkien@lotr.org>
@Immutable
public record EmailAddress(Optional<String> displayName, String localPart, String domain)
extends Record
Represents an email address according to RFC 5322, designed as a modern,
light-weight alternative API to Comparison with
javax.mail.InternetAddress.
For example:
EmailAddress address = EmailAddress.parse("J.R.R. Tolkien <tolkien@lotr.org>");
// address.displayName() => "J.R.R. Tolkien"
// address.localPart()) => "tolkien"
// address.domain() => "lotr.org"
RFC 5322 Compliance Profile
- Address Specification (addr-spec): Supports the standard
local-part@domainformat (RFC 5322 §3.4.1). - Quoted Local-Parts: Fully supports double-quoted local-parts
(RFC 5322 §3.4.1), with backslash-escaped characters. In order to provide a clean,
canonical representation, enclosing quotes are automatically stripped and backslash
escapes are unescaped when stored in the
localPartproperty (e.g.,"john doe" -> "john doe"). While serializing viaaddress()ortoString(), appropriate quotes and escapes are dynamically and safely re-introduced if necessary to maintain syntactical validity under RFC 5322 (since v10.3). - 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.
- Address-List: Supports semicolon as separators; allows real-world variations like trailing commas, two-commas-in-a-row etc.
Comparison with javax.mail.InternetAddress
| Feature | InternetAddress | EmailAddress |
|---|---|---|
| Immutability | Mutable pojo | Immutable record |
| DNS labels | Permissive (allows illegal hyphens) | Rejects wrong-@.com |
| 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:
- Comments (CFWS): (e.g.,
name(comment) <addr>) - De facto obsolete. - Domain Literals: (e.g.,
user@[192.168.1.1]) - IP routing is rarely supported.
- 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 EmailAddressParsesaddressand throwsParser.ParseExceptionif failed.static EmailAddressFor example:EmailAddress.of("user", "mycompany.com").static EmailAddressDeprecated.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
-
of
Parsesaddressand throwsParser.ParseExceptionif failed.- Since:
- 9.9.8
-
parse
Deprecated.Useof(String)instead -
parseAddressList
ParsesaddressListaccording to RFC 5322 and returns an immutable list ofEmailAddress.Both comma (
,) and semicolon (;) are supported as delimiters, with whitespaces ignored. Trailing delimiters are allowed.Empty input will result in an empty list being returned.
- Throws:
Parser.ParseException- ifaddressListis invalid
-
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
Returns the value of thedisplayNamerecord component.- Returns:
- the value of the
displayNamerecord component
-
localPart
-
domain
-
of(String)instead