- java.lang.Object
- 
- jakarta.mail.Address
- 
- jakarta.mail.internet.InternetAddress
 
 
- 
- All Implemented Interfaces:
- java.io.Serializable,- java.lang.Cloneable
 
 public class InternetAddress extends Address implements java.lang.Cloneable This class represents an Internet email address using the syntax of RFC822. Typical address syntax is of the form "user@host.domain" or "Personal Name <user@host.domain>".- Author:
- Bill Shannon, John Mani
- See Also:
- Serialized Form
 
- 
- 
Field SummaryFields Modifier and Type Field Description protected java.lang.StringaddressThe email address.protected java.lang.StringencodedPersonalThe RFC 2047 encoded version of the personal name.protected java.lang.StringpersonalThe personal name.
 - 
Constructor SummaryConstructors Constructor Description InternetAddress()Default constructor.InternetAddress(java.lang.String address)Constructor.InternetAddress(java.lang.String address, boolean strict)Parse the given string and create an InternetAddress.InternetAddress(java.lang.String address, java.lang.String personal)Construct an InternetAddress given the address and personal name.InternetAddress(java.lang.String address, java.lang.String personal, java.lang.String charset)Construct an InternetAddress given the address and personal name.
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Objectclone()Return a copy of this InternetAddress object.booleanequals(java.lang.Object a)The equality operator.java.lang.StringgetAddress()Get the email address.InternetAddress[]getGroup(boolean strict)Return the members of a group address.static InternetAddressgetLocalAddress(Session session)Return an InternetAddress object representing the current user.java.lang.StringgetPersonal()Get the personal name.java.lang.StringgetType()Return the type of this address.inthashCode()Compute a hash code for the address.booleanisGroup()Indicates whether this address is an RFC 822 group address.static InternetAddress[]parse(java.lang.String addresslist)Parse the given comma separated sequence of addresses into InternetAddress objects.static InternetAddress[]parse(java.lang.String addresslist, boolean strict)Parse the given sequence of addresses into InternetAddress objects.static InternetAddress[]parseHeader(java.lang.String addresslist, boolean strict)Parse the given sequence of addresses into InternetAddress objects.voidsetAddress(java.lang.String address)Set the email address.voidsetPersonal(java.lang.String name)Set the personal name.voidsetPersonal(java.lang.String name, java.lang.String charset)Set the personal name.java.lang.StringtoString()Convert this address into a RFC 822 / RFC 2047 encoded address.static java.lang.StringtoString(Address[] addresses)Convert the given array of InternetAddress objects into a comma separated sequence of address strings.static java.lang.StringtoString(Address[] addresses, int used)Convert the given array of InternetAddress objects into a comma separated sequence of address strings.java.lang.StringtoUnicodeString()Returns a properly formatted address (RFC 822 syntax) of Unicode characters.static java.lang.StringtoUnicodeString(Address[] addresses)Convert the given array of InternetAddress objects into a comma separated sequence of address strings.static java.lang.StringtoUnicodeString(Address[] addresses, int used)Convert the given array of InternetAddress objects into a comma separated sequence of address strings.voidvalidate()Validate that this address conforms to the syntax rules of RFC 822.
 
- 
- 
- 
Field Detail- 
addressprotected java.lang.String address The email address.
 - 
personalprotected java.lang.String personal The personal name.
 - 
encodedPersonalprotected java.lang.String encodedPersonal The RFC 2047 encoded version of the personal name.This field and the personalfield track each other, so if a subclass sets one of these fields directly, it should set the other tonull, so that it is suitably recomputed.
 
- 
 - 
Constructor Detail- 
InternetAddresspublic InternetAddress() Default constructor.
 - 
InternetAddresspublic InternetAddress(java.lang.String address) throws AddressExceptionConstructor.Parse the given string and create an InternetAddress. See the parsemethod for details of the parsing. The address is parsed using "strict" parsing. This constructor does not perform the additional syntax checks that theInternetAddress(String address, boolean strict)constructor does whenstrictistrue. This constructor is equivalent toInternetAddress(address, false).- Parameters:
- address- the address in RFC822 format
- Throws:
- AddressException- if the parse failed
 
 - 
InternetAddresspublic InternetAddress(java.lang.String address, boolean strict) throws AddressExceptionParse the given string and create an InternetAddress. Ifstrictis false, the detailed syntax of the address isn't checked.- Parameters:
- address- the address in RFC822 format
- strict- enforce RFC822 syntax
- Throws:
- AddressException- if the parse failed
 
 - 
InternetAddresspublic InternetAddress(java.lang.String address, java.lang.String personal) throws java.io.UnsupportedEncodingExceptionConstruct an InternetAddress given the address and personal name. The address is assumed to be a syntactically valid RFC822 address.- Parameters:
- address- the address in RFC822 format
- personal- the personal name
- Throws:
- java.io.UnsupportedEncodingException- if the personal name can't be encoded in the given charset
 
 - 
InternetAddresspublic InternetAddress(java.lang.String address, java.lang.String personal, java.lang.String charset) throws java.io.UnsupportedEncodingExceptionConstruct an InternetAddress given the address and personal name. The address is assumed to be a syntactically valid RFC822 address.- Parameters:
- address- the address in RFC822 format
- personal- the personal name
- charset- the MIME charset for the name
- Throws:
- java.io.UnsupportedEncodingException- if the personal name can't be encoded in the given charset
 
 
- 
 - 
Method Detail- 
clonepublic java.lang.Object clone() Return a copy of this InternetAddress object.- Overrides:
- clonein class- java.lang.Object
 
 - 
getTypepublic java.lang.String getType() Return the type of this address. The type of an InternetAddress is "rfc822".- Specified by:
- getTypein class- Address
- Returns:
- address type
- See Also:
- InternetAddress
 
 - 
setAddresspublic void setAddress(java.lang.String address) Set the email address.- Parameters:
- address- email address
 
 - 
setPersonalpublic void setPersonal(java.lang.String name, java.lang.String charset) throws java.io.UnsupportedEncodingExceptionSet the personal name. If the name contains non US-ASCII characters, then the name will be encoded using the specified charset as per RFC 2047. If the name contains only US-ASCII characters, no encoding is done and the name is used as is.- Parameters:
- name- personal name
- charset- MIME charset to be used to encode the name as per RFC 2047
- Throws:
- java.io.UnsupportedEncodingException- if the charset encoding fails.
- See Also:
- setPersonal(String)
 
 - 
setPersonalpublic void setPersonal(java.lang.String name) throws java.io.UnsupportedEncodingExceptionSet the personal name. If the name contains non US-ASCII characters, then the name will be encoded using the platform's default charset. If the name contains only US-ASCII characters, no encoding is done and the name is used as is.- Parameters:
- name- personal name
- Throws:
- java.io.UnsupportedEncodingException- if the charset encoding fails.
- See Also:
- setPersonal(String name, String charset)
 
 - 
getAddresspublic java.lang.String getAddress() Get the email address.- Returns:
- email address
 
 - 
getPersonalpublic java.lang.String getPersonal() Get the personal name. If the name is encoded as per RFC 2047, it is decoded and converted into Unicode. If the decoding or conversion fails, the raw data is returned as is.- Returns:
- personal name
 
 - 
toStringpublic java.lang.String toString() Convert this address into a RFC 822 / RFC 2047 encoded address. The resulting string contains only US-ASCII characters, and hence is mail-safe.
 - 
toUnicodeStringpublic java.lang.String toUnicodeString() Returns a properly formatted address (RFC 822 syntax) of Unicode characters.- Returns:
- Unicode address string
 
 - 
equalspublic boolean equals(java.lang.Object a) The equality operator.
 - 
hashCodepublic int hashCode() Compute a hash code for the address.- Overrides:
- hashCodein class- java.lang.Object
 
 - 
toStringpublic static java.lang.String toString(Address[] addresses) Convert the given array of InternetAddress objects into a comma separated sequence of address strings. The resulting string contains only US-ASCII characters, and hence is mail-safe.- Parameters:
- addresses- array of InternetAddress objects
- Returns:
- comma separated string of addresses
- Throws:
- java.lang.ClassCastException- if any address object in the given array is not an InternetAddress object. Note that this is a RuntimeException.
 
 - 
toUnicodeStringpublic static java.lang.String toUnicodeString(Address[] addresses) Convert the given array of InternetAddress objects into a comma separated sequence of address strings. The resulting string contains Unicode characters.- Parameters:
- addresses- array of InternetAddress objects
- Returns:
- comma separated string of addresses
- Throws:
- java.lang.ClassCastException- if any address object in the given array is not an InternetAddress object. Note that this is a RuntimeException.
 
 - 
toStringpublic static java.lang.String toString(Address[] addresses, int used) Convert the given array of InternetAddress objects into a comma separated sequence of address strings. The resulting string contains only US-ASCII characters, and hence is mail-safe.The 'used' parameter specifies the number of character positions already taken up in the field into which the resulting address sequence string is to be inserted. It is used to determine the line-break positions in the resulting address sequence string. - Parameters:
- addresses- array of InternetAddress objects
- used- number of character positions already used, in the field into which the address string is to be inserted.
- Returns:
- comma separated string of addresses
- Throws:
- java.lang.ClassCastException- if any address object in the given array is not an InternetAddress object. Note that this is a RuntimeException.
 
 - 
toUnicodeStringpublic static java.lang.String toUnicodeString(Address[] addresses, int used) Convert the given array of InternetAddress objects into a comma separated sequence of address strings. The resulting string contains Unicode characters.The 'used' parameter specifies the number of character positions already taken up in the field into which the resulting address sequence string is to be inserted. It is used to determine the line-break positions in the resulting address sequence string. - Parameters:
- addresses- array of InternetAddress objects
- used- number of character positions already used, in the field into which the address string is to be inserted.
- Returns:
- comma separated string of addresses
- Throws:
- java.lang.ClassCastException- if any address object in the given array is not an InternetAddress object. Note that this is a RuntimeException.
 
 - 
getLocalAddresspublic static InternetAddress getLocalAddress(Session session) Return an InternetAddress object representing the current user. The entire email address may be specified in the "mail.from" property. If not set, the "mail.user" and "mail.host" properties are tried. If those are not set, the "user.name" property andInetAddress.getLocalHostmethod are tried. Security exceptions that may occur while accessing this information are ignored. If it is not possible to determine an email address, null is returned.- Parameters:
- session- Session object used for property lookup
- Returns:
- current user's email address
 
 - 
parsepublic static InternetAddress[] parse(java.lang.String addresslist) throws AddressException Parse the given comma separated sequence of addresses into InternetAddress objects. Addresses must follow RFC822 syntax.- Parameters:
- addresslist- comma separated address strings
- Returns:
- array of InternetAddress objects
- Throws:
- AddressException- if the parse failed
 
 - 
parsepublic static InternetAddress[] parse(java.lang.String addresslist, boolean strict) throws AddressException Parse the given sequence of addresses into InternetAddress objects. Ifstrictis false, simple email addresses separated by spaces are also allowed. Ifstrictis true, many (but not all) of the RFC822 syntax rules are enforced. In particular, even ifstrictis true, addresses composed of simple names (with no "@domain" part) are allowed. Such "illegal" addresses are not uncommon in real messages.Non-strict parsing is typically used when parsing a list of mail addresses entered by a human. Strict parsing is typically used when parsing address headers in mail messages. - Parameters:
- addresslist- comma separated address strings
- strict- enforce RFC822 syntax
- Returns:
- array of InternetAddress objects
- Throws:
- AddressException- if the parse failed
 
 - 
parseHeaderpublic static InternetAddress[] parseHeader(java.lang.String addresslist, boolean strict) throws AddressException Parse the given sequence of addresses into InternetAddress objects. Ifstrictis false, the full syntax rules for individual addresses are not enforced. Ifstrictis true, many (but not all) of the RFC822 syntax rules are enforced.To better support the range of "invalid" addresses seen in real messages, this method enforces fewer syntax rules than the parsemethod when the strict flag is false and enforces more rules when the strict flag is true. If the strict flag is false and the parse is successful in separating out an email address or addresses, the syntax of the addresses themselves is not checked.- Parameters:
- addresslist- comma separated address strings
- strict- enforce RFC822 syntax
- Returns:
- array of InternetAddress objects
- Throws:
- AddressException- if the parse failed
 
 - 
validatepublic void validate() throws AddressExceptionValidate that this address conforms to the syntax rules of RFC 822. The current implementation checks many, but not all, syntax rules. Note that even though the syntax of the address may be correct, there's no guarantee that a mailbox of that name exists.- Throws:
- AddressException- if the address isn't valid.
 
 - 
isGrouppublic boolean isGroup() Indicates whether this address is an RFC 822 group address. Note that a group address is different than the mailing list addresses supported by most mail servers. Group addresses are rarely used; see RFC 822 for details.- Returns:
- true if this address represents a group
 
 - 
getGrouppublic InternetAddress[] getGroup(boolean strict) throws AddressException Return the members of a group address. A group may have zero, one, or more members. If this address is not a group, null is returned. Thestrictparameter controls whether the group list is parsed using strict RFC 822 rules or not. The parsing is done using theparseHeadermethod.- Parameters:
- strict- use strict RFC 822 rules?
- Returns:
- array of InternetAddress objects, or null
- Throws:
- AddressException- if the group list can't be parsed
 
 
- 
 
-