public class InternetAddress extends Address implements java.lang.Cloneable
Modifier and Type | Field and Description |
---|---|
protected java.lang.String |
address |
protected java.lang.String |
encodedPersonal
The RFC 2047 encoded version of the personal name.
|
private static boolean |
ignoreBogusGroupName |
protected java.lang.String |
personal
The personal name.
|
private static java.lang.String |
rfc822phrase |
private static long |
serialVersionUID |
private static java.lang.String |
specialsNoDot |
private static java.lang.String |
specialsNoDotNoAt |
Constructor and 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.
|
Modifier and Type | Method and Description |
---|---|
(package private) static InternetAddress |
_getLocalAddress(Session session)
A package-private version of getLocalAddress that doesn't swallow
the exception.
|
private static void |
checkAddress(java.lang.String addr,
boolean routeAddr,
boolean validate)
Check that the address is a valid "mailbox" per RFC822.
|
java.lang.Object |
clone()
Return a copy of this InternetAddress object.
|
boolean |
equals(java.lang.Object a)
The equality operator.
|
java.lang.String |
getAddress()
Get the email address.
|
InternetAddress[] |
getGroup(boolean strict)
Return the members of a group address.
|
static InternetAddress |
getLocalAddress(Session session)
Return an InternetAddress object representing the current user.
|
private static java.lang.String |
getLocalHostName()
Get the local host name from InetAddress and return it in a form
suitable for use in an email address.
|
java.lang.String |
getPersonal()
Get the personal name.
|
java.lang.String |
getType()
Return the type of this address.
|
int |
hashCode()
Compute a hash code for the address.
|
private static int |
indexOfAny(java.lang.String s,
java.lang.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(java.lang.String s,
java.lang.String any,
int start) |
boolean |
isGroup()
Indicates whether this address is an RFC 822 group address.
|
private static boolean |
isInetAddressLiteral(java.lang.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.
|
private boolean |
isSimple()
Is this a "simple" address? Simple addresses don't contain quotes
or any RFC822 special characters other than '@' and '.'.
|
private static int |
lengthOfFirstSegment(java.lang.String s) |
private static int |
lengthOfLastSegment(java.lang.String s,
int used) |
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.
|
private static InternetAddress[] |
parse(java.lang.String s,
boolean strict,
boolean parseHdr) |
static InternetAddress[] |
parseHeader(java.lang.String addresslist,
boolean strict)
Parse the given sequence of addresses into InternetAddress
objects.
|
private static java.lang.String |
quotePhrase(java.lang.String phrase) |
void |
setAddress(java.lang.String address)
Set the email address.
|
void |
setPersonal(java.lang.String name)
Set the personal name.
|
void |
setPersonal(java.lang.String name,
java.lang.String charset)
Set the personal name.
|
java.lang.String |
toString()
Convert this address into a RFC 822 / RFC 2047 encoded address.
|
static java.lang.String |
toString(Address[] addresses)
Convert the given array of InternetAddress objects into
a comma separated sequence of address strings.
|
static java.lang.String |
toString(Address[] addresses,
int used)
Convert the given array of InternetAddress objects into
a comma separated sequence of address strings.
|
java.lang.String |
toUnicodeString()
Returns a properly formatted address (RFC 822 syntax) of
Unicode characters.
|
private static java.lang.String |
unquote(java.lang.String s) |
void |
validate()
Validate that this address conforms to the syntax rules of
RFC 822.
|
protected java.lang.String address
protected java.lang.String personal
protected java.lang.String encodedPersonal
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.
private static final long serialVersionUID
private static final boolean ignoreBogusGroupName
private static final java.lang.String rfc822phrase
private static final java.lang.String specialsNoDotNoAt
private static final java.lang.String specialsNoDot
public InternetAddress()
public InternetAddress(java.lang.String address) throws AddressException
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)
.
address
- the address in RFC822 formatAddressException
- if the parse failedpublic InternetAddress(java.lang.String address, boolean strict) throws AddressException
strict
is false, the detailed syntax of the
address isn't checked.address
- the address in RFC822 formatstrict
- enforce RFC822 syntaxAddressException
- if the parse failedpublic InternetAddress(java.lang.String address, java.lang.String personal) throws java.io.UnsupportedEncodingException
address
- the address in RFC822 formatpersonal
- the personal namejava.io.UnsupportedEncodingException
- if the personal name
can't be encoded in the given charsetpublic InternetAddress(java.lang.String address, java.lang.String personal, java.lang.String charset) throws java.io.UnsupportedEncodingException
address
- the address in RFC822 formatpersonal
- the personal namecharset
- the MIME charset for the namejava.io.UnsupportedEncodingException
- if the personal name
can't be encoded in the given charsetpublic java.lang.Object clone()
clone
in class java.lang.Object
public java.lang.String getType()
getType
in class Address
InternetAddress
public void setAddress(java.lang.String address)
address
- email addresspublic void setPersonal(java.lang.String name, java.lang.String charset) throws java.io.UnsupportedEncodingException
name
- personal namecharset
- MIME charset to be used to encode the name as
per RFC 2047java.io.UnsupportedEncodingException
- if the charset encoding
fails.setPersonal(String)
public void setPersonal(java.lang.String name) throws java.io.UnsupportedEncodingException
name
- personal namejava.io.UnsupportedEncodingException
- if the charset encoding
fails.setPersonal(String name, String charset)
public java.lang.String getAddress()
public java.lang.String getPersonal()
public java.lang.String toString()
public java.lang.String toUnicodeString()
private static java.lang.String quotePhrase(java.lang.String phrase)
private static java.lang.String unquote(java.lang.String s)
public boolean equals(java.lang.Object a)
public int hashCode()
hashCode
in class java.lang.Object
public static java.lang.String toString(Address[] addresses)
addresses
- array of InternetAddress objectsjava.lang.ClassCastException
- if any address object in the
given array is not an InternetAddress object. Note
that this is a RuntimeException.public static java.lang.String toString(Address[] addresses, int used)
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.
addresses
- array of InternetAddress objectsused
- number of character positions already used, in
the field into which the address string is to
be inserted.java.lang.ClassCastException
- if any address object in the
given array is not an InternetAddress object. Note
that this is a RuntimeException.private static int lengthOfFirstSegment(java.lang.String s)
private static int lengthOfLastSegment(java.lang.String s, int used)
public static InternetAddress getLocalAddress(Session session)
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.session
- Session object used for property lookupstatic InternetAddress _getLocalAddress(Session session) throws java.lang.SecurityException, AddressException, java.net.UnknownHostException
java.lang.SecurityException
AddressException
java.net.UnknownHostException
private static java.lang.String getLocalHostName() throws java.net.UnknownHostException
java.net.UnknownHostException
private static boolean isInetAddressLiteral(java.lang.String addr)
public static InternetAddress[] parse(java.lang.String addresslist) throws AddressException
addresslist
- comma separated address stringsAddressException
- if the parse failedpublic static InternetAddress[] parse(java.lang.String addresslist, boolean strict) throws AddressException
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.
addresslist
- comma separated address stringsstrict
- enforce RFC822 syntaxAddressException
- if the parse failedpublic static InternetAddress[] parseHeader(java.lang.String addresslist, boolean strict) throws AddressException
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.
addresslist
- comma separated address stringsstrict
- enforce RFC822 syntaxAddressException
- if the parse failedprivate static InternetAddress[] parse(java.lang.String s, boolean strict, boolean parseHdr) throws AddressException
AddressException
public void validate() throws AddressException
AddressException
- if the address isn't valid.private static void checkAddress(java.lang.String addr, boolean routeAddr, boolean validate) throws AddressException
AddressException
private boolean isSimple()
public boolean isGroup()
public InternetAddress[] getGroup(boolean strict) throws AddressException
strict
parameter controls whether
the group list is parsed using strict RFC 822 rules or not.
The parsing is done using the parseHeader
method.strict
- use strict RFC 822 rules?AddressException
- if the group list can't be parsedprivate static int indexOfAny(java.lang.String s, java.lang.String any)
private static int indexOfAny(java.lang.String s, java.lang.String any, int start)