Package javax.mail.internet
Class InternetAddress
- java.lang.Object
-
- javax.mail.Address
-
- javax.mail.internet.InternetAddress
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable
public class InternetAddress extends Address implements java.lang.Cloneable
An RFC 822 address.- Version:
- 1.4
- Author:
- Chris Burdess
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected java.lang.StringaddressThe string representation of the address.protected java.lang.StringencodedPersonalThe RFC 2047 encoded version of the personal name.protected java.lang.StringpersonalThe personal name.
-
Constructor Summary
Constructors Constructor Description InternetAddress()Constructor for an empty address.InternetAddress(java.lang.String address)Constructor with an RFC 822 string representation of the address.InternetAddress(java.lang.String address, boolean strict)Constructor with an RFC 822 string representation of the address.InternetAddress(java.lang.String address, java.lang.String personal)Constructor with an address and personal name.InternetAddress(java.lang.String address, java.lang.String personal, java.lang.String charset)Construct with an address and personal name.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Objectclone()Returns a copy of this address.booleanequals(java.lang.Object other)java.lang.StringgetAddress()Returns the email address.InternetAddress[]getGroup(boolean strict)Returns the members of a group address.static InternetAddressgetLocalAddress(Session session)Returns an InternetAddress object representing the current user.java.lang.StringgetPersonal()Returns the personal name.java.lang.StringgetType()Returns the type of this address.inthashCode()booleanisGroup()Indicates whether this address is an RFC 822 group address.static InternetAddress[]parse(java.lang.String addresslist)Parses the given comma-separated sequence of RFC 822 addresses into InternetAddresses.static InternetAddress[]parse(java.lang.String addresslist, boolean strict)Parses the given comma-separated sequence of RFC 822 addresses into InternetAddresses.static InternetAddress[]parseHeader(java.lang.String addresslist, boolean strict)Parses the given comma-separated sequence of RFC 822 addresses into InternetAddresses.voidsetAddress(java.lang.String address)Sets the email address.voidsetPersonal(java.lang.String name)Sets the personal name.voidsetPersonal(java.lang.String name, java.lang.String charset)Sets the personal name.java.lang.StringtoString()Returns the RFC 822 / RFC 2047 string representation of this address.static java.lang.StringtoString(Address[] addresses)Converts the given array of InternetAddresses into a comma-separated sequence of address strings.static java.lang.StringtoString(Address[] addresses, int used)Converts the given array of InternetAddresses into a comma-separated sequence of address strings.java.lang.StringtoUnicodeString()Returns the RFC 822 string representation of this address.voidvalidate()Validate this address according to the syntax rules of RFC 822.
-
-
-
Constructor Detail
-
InternetAddress
public InternetAddress()
Constructor for an empty address.
-
InternetAddress
public InternetAddress(java.lang.String address) throws AddressExceptionConstructor with an RFC 822 string representation of the address. Note that this parses the address in non-strict mode: this is for compatibility with implementations and not with the JavaMail specification.- Parameters:
address- the address in RFC 822 format- Throws:
AddressException- if the parse failed
-
InternetAddress
public InternetAddress(java.lang.String address, boolean strict) throws AddressExceptionConstructor with an RFC 822 string representation of the address.- Parameters:
address- the address in RFC 822 formatstrict- enforce RFC 822 syntax- Throws:
AddressException- if the parse failed- Since:
- JavaMail 1.3
-
InternetAddress
public InternetAddress(java.lang.String address, java.lang.String personal) throws java.io.UnsupportedEncodingExceptionConstructor with an address and personal name. The address is assumed to be syntactically valid according to RFC 822.- Parameters:
address- the address in RFC 822 formatpersonal- the personal name- Throws:
java.io.UnsupportedEncodingException
-
InternetAddress
public InternetAddress(java.lang.String address, java.lang.String personal, java.lang.String charset) throws java.io.UnsupportedEncodingExceptionConstruct with an address and personal name. The address is assumed to be syntactically valid according to RFC 822.- Parameters:
address- the address in RFC 822 formatpersonal- the personal namecharset- the charset for the personal name- Throws:
java.io.UnsupportedEncodingException
-
-
Method Detail
-
clone
public java.lang.Object clone()
Returns a copy of this address.- Overrides:
clonein classjava.lang.Object
-
getType
public java.lang.String getType()
Returns the type of this address. The type of anInternetAddressis "rfc822".
-
isGroup
public boolean isGroup()
Indicates whether this address is an RFC 822 group address. Group addresses are not mailing list addresses and are rarely used; see RFC 822 for details.- Since:
- JavaMail 1.3
-
getGroup
public InternetAddress[] getGroup(boolean strict) throws AddressException
Returns the members of a group address. A group may have any number of members. If this address is not a group, this method returnsnull.- Throws:
AddressException- if a parse error occurs- Since:
- JavaMail 1.3
-
setAddress
public void setAddress(java.lang.String address)
Sets the email address.
-
setPersonal
public void setPersonal(java.lang.String name, java.lang.String charset) throws java.io.UnsupportedEncodingExceptionSets the personal name. If the name contains non US-ASCII characters, it will be encoded using the specified charset as per RFC 2047.- Parameters:
name- the personal namecharset- the charset to be used for any encodingUnsupportedEncodingException- if charset encoding fails- Throws:
java.io.UnsupportedEncodingException
-
setPersonal
public void setPersonal(java.lang.String name) throws java.io.UnsupportedEncodingExceptionSets the personal name. If the name contains non US-ASCII characters, it will be encoded using the platform default charset.- Parameters:
name- the personal name- Throws:
java.io.UnsupportedEncodingException- if charset encoding fails
-
getAddress
public java.lang.String getAddress()
Returns the email address.
-
getPersonal
public java.lang.String getPersonal()
Returns the personal name.
-
validate
public void validate() throws AddressExceptionValidate this address according to the syntax rules of RFC 822. This implementation checks many but not all of the syntax rules.- Throws:
AddressException- if the address is invalid- Since:
- JavaMail 1.3
-
toString
public java.lang.String toString()
Returns the RFC 822 / RFC 2047 string representation of this address. The resulting string contains only US-ASCII characters, and is therefore mail-safe.
-
toUnicodeString
public java.lang.String toUnicodeString()
Returns the RFC 822 string representation of this address. The returned string may contain unencoded Unicode characters and may therefore not be mail-safe.
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
toString
public static java.lang.String toString(Address[] addresses)
Converts the given array of InternetAddresses into a comma-separated sequence of address strings. The resulting string contains only US-ASCII characters, and is therefore mail-safe.- Parameters:
addresses- the InternetAddresses- Throws:
java.lang.ClassCastException- if any of the specified addresses is not an InternetAddress
-
toString
public static java.lang.String toString(Address[] addresses, int used)
Converts the given array of InternetAddresses into a comma-separated sequence of address strings. The resulting string contains only US-ASCII characters, and is therefore mail-safe.- Parameters:
addresses- the InternetAddressesused- the number of character positions already used, in the field into which the address string is to be inserted- Throws:
java.lang.ClassCastException- if any of the specified addresses is not an InternetAddress
-
getLocalAddress
public static InternetAddress getLocalAddress(Session session)
Returns an InternetAddress object representing the current user. This information is determined from the following locations, in order of preference:- the session property
mail.from - the session properties
mail.useroruser.name, andmail.host - the system property
user.nameand the hostname of localhost as determined byInetAddress.getLocalHost
- Parameters:
session- the session
- the session property
-
parse
public static InternetAddress[] parse(java.lang.String addresslist) throws AddressException
Parses the given comma-separated sequence of RFC 822 addresses into InternetAddresses.- Parameters:
addresslist- the comma-separated addresses- Throws:
AddressException- if the parse failed
-
parse
public static InternetAddress[] parse(java.lang.String addresslist, boolean strict) throws AddressException
Parses the given comma-separated sequence of RFC 822 addresses into InternetAddresses. Ifstrictis false, simple email addresses separated by spaces are also allowed. Ifstrictis true, many (but not all) of the RFC 822 syntax rules are enforced. Even ifstrictis true, addresses composed of simple names (with no "@domain" part) are allowed.- Parameters:
addresslist- the comma-separated addressesstrict- whether to enforce RFC 822 syntax- Throws:
AddressException- if the parse failed
-
parseHeader
public static InternetAddress[] parseHeader(java.lang.String addresslist, boolean strict) throws AddressException
Parses the given comma-separated sequence of RFC 822 addresses into InternetAddresses. Ifstrictis false, simple email addresses separated by spaces are also allowed. Ifstrictis true, many (but not all) of the RFC 822 syntax rules are enforced.- Parameters:
addresslist- the comma-separated addressesstrict- whether to enforce RFC 822 syntax- Throws:
AddressException- if the parse failed- Since:
- JavaMail 1.3
-
-