Top Description Fields Constructors Methods
jakarta.mail.internet

public Class InternetAddress

extends Address
implements Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable
Imports
java.io.UnsupportedEncodingException, java.net.InetAddress, .UnknownHostException, java.util.List, .ArrayList, .StringTokenizer, .Locale, java.nio.charset.StandardCharsets, jakarta.mail.*

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>".
Authors
Bill Shannon, John Mani

Field Summary

Modifier and TypeField and Description
protected String
private static final boolean
protected String
encodedPersonal

The RFC 2047 encoded version of the personal name.

private static final boolean
protected String
personal

The personal name.

private static final String
private static final long
private static final String
private static final String
private static final boolean

Constructor Summary

AccessConstructor and Description
public
InternetAddress()

Default constructor.

public
InternetAddress(String
the address in RFC822 format
address
)

Constructor.

public
InternetAddress(String
the address in RFC822 format
address
,
boolean
enforce RFC822 syntax
strict
)

Parse the given string and create an InternetAddress.

public
InternetAddress(String
the address in RFC822 format
address
,
String
the personal name
personal
)

Construct an InternetAddress given the address and personal name.

public
InternetAddress(String
the address in RFC822 format
address
,
String
the personal name
personal
,
String
the MIME charset for the name
charset
)

Construct an InternetAddress given the address and personal name.

Method Summary

Modifier and TypeMethod and Description
pack-priv static InternetAddress
_getLocalAddress(Session session)

A package-private version of getLocalAddress that doesn't swallow the exception.

private static void
checkAddress(String addr, boolean routeAddr, boolean validate)

Check that the address is a valid "mailbox" per RFC822.

public Object
clone()

Overrides java.lang.Object.clone.

Return a copy of this InternetAddress object.
public boolean
equals(Object
Address object
a
)

Implements abstract jakarta.mail.Address.equals.

The equality operator.
public String

Returns:

email address
getAddress
()

Get the email address.

public InternetAddress[]

Returns:

array of InternetAddress objects, or null
getGroup
(boolean
use strict RFC 822 rules?
strict
)

Return the members of a group address.

public static InternetAddress

Returns:

current user's email address
getLocalAddress
(Session
Session object used for property lookup
session
)

Return an InternetAddress object representing the current user.

private static String
getLocalHostName()

Get the local host name from InetAddress and return it in a form suitable for use in an email address.

public String

Returns:

personal name
getPersonal
()

Get the personal name.

public String
getType()

Implements abstract jakarta.mail.Address.getType.

Return the type of this address.
public int
hashCode()

Overrides java.lang.Object.hashCode.

Compute a hash code for the address.
private static int
indexOfAny(String s, String any)

Return the first index of any of the characters in "any" in "s", or -1 if none are found.

private static int
indexOfAny(String s, String any, int start)

public boolean

Returns:

true if this address represents a group
isGroup
()

Indicates whether this address is an RFC 822 group address.

private static boolean
isInetAddressLiteral(String addr)

Is the address an IPv4 or IPv6 address literal, which needs to be enclosed in "[]" in an email address?

private boolean
isSimple()

Is this a "simple" address?

private static int
private static int
public static InternetAddress[]

Returns:

array of InternetAddress objects
parse
(String
comma separated address strings
addresslist
)

Parse the given comma separated sequence of addresses into InternetAddress objects.

public static InternetAddress[]

Returns:

array of InternetAddress objects
parse
(String
comma separated address strings
addresslist
,
boolean
enforce RFC822 syntax
strict
)

Parse the given sequence of addresses into InternetAddress objects.

private static InternetAddress[]
parse(String s, boolean strict, boolean parseHdr)

public static InternetAddress[]

Returns:

array of InternetAddress objects
parseHeader
(String
comma separated address strings
addresslist
,
boolean
enforce RFC822 syntax
strict
)

Parse the given sequence of addresses into InternetAddress objects.

private static String
public void
setAddress(String
email address
address
)

Set the email address.

public void
setPersonal(String
personal name
name
,
String
MIME charset to be used to encode the name as per RFC 2047
charset
)

Set the personal name.

public void
setPersonal(String
personal name
name
)

Set the personal name.

public String

Returns:

possibly encoded address string
toString
()

Implements abstract jakarta.mail.Address.toString.

Convert this address into a RFC 822 / RFC 2047 encoded address.
public static String

Returns:

comma separated string of addresses
toString
(Address[]
array of InternetAddress objects
addresses
)

Convert the given array of InternetAddress objects into a comma separated sequence of address strings.

public static String

Returns:

comma separated string of addresses
toString
(Address[]
array of InternetAddress objects
addresses
,
int
number of character positions already used, in the field into which the address string is to be inserted.
used
)

Convert the given array of InternetAddress objects into a comma separated sequence of address strings.

public String

Returns:

Unicode address string
toUnicodeString
()

Returns a properly formatted address (RFC 822 syntax) of Unicode characters.

public static String

Returns:

comma separated string of addresses
toUnicodeString
(Address[]
array of InternetAddress objects
addresses
)

Convert the given array of InternetAddress objects into a comma separated sequence of address strings.

public static String

Returns:

comma separated string of addresses
toUnicodeString
(Address[]
array of InternetAddress objects
addresses
,
int
number of character positions already used, in the field into which the address string is to be inserted.
used
)

Convert the given array of InternetAddress objects into a comma separated sequence of address strings.

private static String
public void
validate()

Validate that this address conforms to the syntax rules of RFC 822.

Field Detail

addressback to summary
protected String address
allowUtf8back to summary
private static final boolean allowUtf8
encodedPersonalback to summary
protected String encodedPersonal

The RFC 2047 encoded version of the personal name.

This field and the personal field track each other, so if a subclass sets one of these fields directly, it should set the other to null, so that it is suitably recomputed.

ignoreBogusGroupNameback to summary
private static final boolean ignoreBogusGroupName
personalback to summary
protected String personal

The personal name.

rfc822phraseback to summary
private static final String rfc822phrase
serialVersionUIDback to summary
private static final long serialVersionUID

Hides jakarta.mail.Address.serialVersionUID.

specialsNoDotback to summary
private static final String specialsNoDot
specialsNoDotNoAtback to summary
private static final String specialsNoDotNoAt
useCanonicalHostNameback to summary
private static final boolean useCanonicalHostName

Constructor Detail

InternetAddressback to summary
public InternetAddress()

Default constructor.

InternetAddressback to summary
public InternetAddress(String address) throws AddressException

Constructor.

Parse the given string and create an InternetAddress. See the parse method for details of the parsing. The address is parsed using "strict" parsing. This constructor does not perform the additional syntax checks that the InternetAddress(String address, boolean strict) constructor does when strict is true. This constructor is equivalent to InternetAddress(address, false).

Parameters
address:String

the address in RFC822 format

Exceptions
AddressException:
if the parse failed
InternetAddressback to summary
public InternetAddress(String address, boolean strict) throws AddressException

Parse the given string and create an InternetAddress. If strict is false, the detailed syntax of the address isn't checked.

Parameters
address:String

the address in RFC822 format

strict:boolean

enforce RFC822 syntax

Exceptions
AddressException:
if the parse failed
Since
JavaMail 1.3
InternetAddressback to summary
public InternetAddress(String address, String personal) throws UnsupportedEncodingException

Construct an InternetAddress given the address and personal name. The address is assumed to be a syntactically valid RFC822 address.

Parameters
address:String

the address in RFC822 format

personal:String

the personal name

Exceptions
UnsupportedEncodingException:
if the personal name can't be encoded in the given charset
InternetAddressback to summary
public InternetAddress(String address, String personal, String charset) throws UnsupportedEncodingException

Construct an InternetAddress given the address and personal name. The address is assumed to be a syntactically valid RFC822 address.

Parameters
address:String

the address in RFC822 format

personal:String

the personal name

charset:String

the MIME charset for the name

Exceptions
UnsupportedEncodingException:
if the personal name can't be encoded in the given charset

Method Detail

_getLocalAddressback to summary
pack-priv static InternetAddress _getLocalAddress(Session session) throws SecurityException, AddressException, UnknownHostException

A package-private version of getLocalAddress that doesn't swallow the exception. Used by MimeMessage.setFrom() to report the reason for the failure.

checkAddressback to summary
private static void checkAddress(String addr, boolean routeAddr, boolean validate) throws AddressException

Check that the address is a valid "mailbox" per RFC822. (We also allow simple names.) XXX - much more to check XXX - doesn't handle domain-literals properly (but no one uses them)

cloneback to summary
public Object clone()

Overrides java.lang.Object.clone.

Return a copy of this InternetAddress object.

Returns:Object

Doc from java.lang.Object.clone.

a clone of this instance.

Annotations
@Override
Since
JavaMail 1.2
equalsback to summary
public boolean equals(Object a)

Implements abstract jakarta.mail.Address.equals.

The equality operator.

Parameters
a:Object

Doc from jakarta.mail.Address.equals.

Address object

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getAddressback to summary
public String getAddress()

Get the email address.

Returns:String

email address

getGroupback to summary
public 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. The strict parameter controls whether the group list is parsed using strict RFC 822 rules or not. The parsing is done using the parseHeader method.

Parameters
strict:boolean

use strict RFC 822 rules?

Returns:InternetAddress[]

array of InternetAddress objects, or null

Exceptions
AddressException:
if the group list can't be parsed
Since
JavaMail 1.3
getLocalAddressback to summary
public 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 and InetAddress.getLocalHost method 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

Session object used for property lookup

Returns:InternetAddress

current user's email address

getLocalHostNameback to summary
private static String getLocalHostName() throws UnknownHostException

Get the local host name from InetAddress and return it in a form suitable for use in an email address.

getPersonalback to summary
public 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:String

personal name

getTypeback to summary
public String getType()

Implements abstract jakarta.mail.Address.getType.

Return the type of this address. The type of an InternetAddress is "rfc822".

Returns:String

Doc from jakarta.mail.Address.getType.

address type

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Compute a hash code for the address.

Returns:int

Doc from java.lang.Object.hashCode.

a hash code value for this object.

Annotations
@Override
indexOfAnyback to summary
private static int indexOfAny(String s, String any)

Return the first index of any of the characters in "any" in "s", or -1 if none are found. This should be a method on String.

indexOfAnyback to summary
private static int indexOfAny(String s, String any, int start)
isGroupback to summary
public 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:boolean

true if this address represents a group

Since
JavaMail 1.3
isInetAddressLiteralback to summary
private static boolean isInetAddressLiteral(String addr)

Is the address an IPv4 or IPv6 address literal, which needs to be enclosed in "[]" in an email address? IPv4 literals contain decimal digits and dots, IPv6 literals contain hex digits, dots, and colons. We're lazy and don't check the exact syntax, just the allowed characters; strings that have only the allowed characters in a literal but don't meet the syntax requirements for a literal definitely can't be a host name and thus will fail later when used as an address literal.

isSimpleback to summary
private boolean isSimple()

Is this a "simple" address? Simple addresses don't contain quotes or any RFC822 special characters other than '@' and '.'.

lengthOfFirstSegmentback to summary
private static int lengthOfFirstSegment(String s)
lengthOfLastSegmentback to summary
private static int lengthOfLastSegment(String s, int used)
parseback to summary
public static InternetAddress[] parse(String addresslist) throws AddressException

Parse the given comma separated sequence of addresses into InternetAddress objects. Addresses must follow RFC822 syntax.

Parameters
addresslist:String

comma separated address strings

Returns:InternetAddress[]

array of InternetAddress objects

Exceptions
AddressException:
if the parse failed
parseback to summary
public static InternetAddress[] parse(String addresslist, boolean strict) throws AddressException

Parse the given sequence of addresses into InternetAddress objects. If strict is false, simple email addresses separated by spaces are also allowed. If strict is true, many (but not all) of the RFC822 syntax rules are enforced. In particular, even if strict is 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:String

comma separated address strings

strict:boolean

enforce RFC822 syntax

Returns:InternetAddress[]

array of InternetAddress objects

Exceptions
AddressException:
if the parse failed
parseback to summary
private static InternetAddress[] parse(String s, boolean strict, boolean parseHdr) throws AddressException
Annotations
@SuppressWarnings:fallthrough
parseHeaderback to summary
public static InternetAddress[] parseHeader(String addresslist, boolean strict) throws AddressException

Parse the given sequence of addresses into InternetAddress objects. If strict is false, the full syntax rules for individual addresses are not enforced. If strict is 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 parse method 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:String

comma separated address strings

strict:boolean

enforce RFC822 syntax

Returns:InternetAddress[]

array of InternetAddress objects

Exceptions
AddressException:
if the parse failed
Since
JavaMail 1.3
quotePhraseback to summary
private static String quotePhrase(String phrase)
setAddressback to summary
public void setAddress(String address)

Set the email address.

Parameters
address:String

email address

setPersonalback to summary
public void setPersonal(String name, String charset) throws UnsupportedEncodingException

Set 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:String

personal name

charset:String

MIME charset to be used to encode the name as per RFC 2047

Exceptions
UnsupportedEncodingException:
if the charset encoding fails.
See Also
setPersonal(String)
setPersonalback to summary
public void setPersonal(String name) throws UnsupportedEncodingException

Set 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:String

personal name

Exceptions
UnsupportedEncodingException:
if the charset encoding fails.
See Also
setPersonal(String name, String charset)
toStringback to summary
public String toString()

Implements abstract jakarta.mail.Address.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.

Returns:String

possibly encoded address string

Annotations
@Override
toStringback to summary
public static 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:Address[]

array of InternetAddress objects

Returns:String

comma separated string of addresses

Exceptions
ClassCastException:
if any address object in the given array is not an InternetAddress object. Note that this is a RuntimeException.
toStringback to summary
public static 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:Address[]

array of InternetAddress objects

used:int

number of character positions already used, in the field into which the address string is to be inserted.

Returns:String

comma separated string of addresses

Exceptions
ClassCastException:
if any address object in the given array is not an InternetAddress object. Note that this is a RuntimeException.
toUnicodeStringback to summary
public String toUnicodeString()

Returns a properly formatted address (RFC 822 syntax) of Unicode characters.

Returns:String

Unicode address string

Since
JavaMail 1.2
toUnicodeStringback to summary
public static 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:Address[]

array of InternetAddress objects

Returns:String

comma separated string of addresses

Exceptions
ClassCastException:
if any address object in the given array is not an InternetAddress object. Note that this is a RuntimeException.
Since
JavaMail 1.6
toUnicodeStringback to summary
public static 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:Address[]

array of InternetAddress objects

used:int

number of character positions already used, in the field into which the address string is to be inserted.

Returns:String

comma separated string of addresses

Exceptions
ClassCastException:
if any address object in the given array is not an InternetAddress object. Note that this is a RuntimeException.
Since
JavaMail 1.6
unquoteback to summary
private static String unquote(String s)
validateback to summary
public void validate() throws AddressException

Validate 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.

Exceptions
AddressException:
if the address isn't valid.
Since
JavaMail 1.3