javax.mail.internet
Class InternetAddress

java.lang.Object
  extended by javax.mail.Address
      extended by javax.mail.internet.InternetAddress
All Implemented Interfaces:
Serializable, Cloneable

public class InternetAddress
extends Address
implements Cloneable

An RFC 822 address.

Version:
1.4
Author:
Chris Burdess
See Also:
Serialized Form

Field Summary
protected  String address
          The string representation of the address.
protected  String encodedPersonal
          The RFC 2047 encoded version of the personal name.
protected  String personal
          The personal name.
 
Constructor Summary
InternetAddress()
          Constructor for an empty address.
InternetAddress(String address)
          Constructor with an RFC 822 string representation of the address.
InternetAddress(String address, boolean strict)
          Constructor with an RFC 822 string representation of the address.
InternetAddress(String address, String personal)
          Constructor with an address and personal name.
InternetAddress(String address, String personal, String charset)
          Construct with an address and personal name.
 
Method Summary
 Object clone()
          Returns a copy of this address.
 boolean equals(Object other)
           
 String getAddress()
          Returns the email address.
 InternetAddress[] getGroup(boolean strict)
          Returns the members of a group address.
static InternetAddress getLocalAddress(Session session)
          Returns an InternetAddress object representing the current user.
 String getPersonal()
          Returns the personal name.
 String getType()
          Returns the type of this address.
 int hashCode()
           
 boolean isGroup()
          Indicates whether this address is an RFC 822 group address.
static InternetAddress[] parse(String addresslist)
          Parses the given comma-separated sequence of RFC 822 addresses into InternetAddresses.
static InternetAddress[] parse(String addresslist, boolean strict)
          Parses the given comma-separated sequence of RFC 822 addresses into InternetAddresses.
static InternetAddress[] parseHeader(String addresslist, boolean strict)
          Parses the given comma-separated sequence of RFC 822 addresses into InternetAddresses.
 void setAddress(String address)
          Sets the email address.
 void setPersonal(String name)
          Sets the personal name.
 void setPersonal(String name, String charset)
          Sets the personal name.
 String toString()
          Returns the RFC 822 / RFC 2047 string representation of this address.
static String toString(Address[] addresses)
          Converts the given array of InternetAddresses into a comma-separated sequence of address strings.
static String toString(Address[] addresses, int used)
          Converts the given array of InternetAddresses into a comma-separated sequence of address strings.
 String toUnicodeString()
          Returns the RFC 822 string representation of this address.
 void validate()
          Validate this address according to the syntax rules of RFC 822.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

address

protected String address
The string representation of the address.


personal

protected String personal
The personal name.


encodedPersonal

protected String encodedPersonal
The RFC 2047 encoded version of the personal name.

Constructor Detail

InternetAddress

public InternetAddress()
Constructor for an empty address.


InternetAddress

public InternetAddress(String address)
                throws AddressException
Constructor 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(String address,
                       boolean strict)
                throws AddressException
Constructor with an RFC 822 string representation of the address.

Parameters:
address - the address in RFC 822 format
strict - enforce RFC 822 syntax
Throws:
AddressException - if the parse failed
Since:
JavaMail 1.3

InternetAddress

public InternetAddress(String address,
                       String personal)
                throws UnsupportedEncodingException
Constructor 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 format
personal - the personal name
Throws:
UnsupportedEncodingException

InternetAddress

public InternetAddress(String address,
                       String personal,
                       String charset)
                throws UnsupportedEncodingException
Construct 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 format
personal - the personal name
charset - the charset for the personal name
Throws:
UnsupportedEncodingException
Method Detail

clone

public Object clone()
Returns a copy of this address.

Overrides:
clone in class Object

getType

public String getType()
Returns the type of this address. The type of an InternetAddress is "rfc822".

Specified by:
getType in class Address

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 returns null.

Throws:
AddressException - if a parse error occurs
Since:
JavaMail 1.3

setAddress

public void setAddress(String address)
Sets the email address.


setPersonal

public void setPersonal(String name,
                        String charset)
                 throws UnsupportedEncodingException
Sets 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 name
charset - the charset to be used for any encoding
UnsupportedEncodingException - if charset encoding fails
Throws:
UnsupportedEncodingException

setPersonal

public void setPersonal(String name)
                 throws UnsupportedEncodingException
Sets 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:
UnsupportedEncodingException - if charset encoding fails

getAddress

public String getAddress()
Returns the email address.


getPersonal

public String getPersonal()
Returns the personal name.


validate

public void validate()
              throws AddressException
Validate 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 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.

Specified by:
toString in class Address

toUnicodeString

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


equals

public boolean equals(Object other)
Specified by:
equals in class Address

hashCode

public int hashCode()
Overrides:
hashCode in class Object

toString

public static 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:
ClassCastException - if any of the specified addresses is not an InternetAddress

toString

public static 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 InternetAddresses
used - the number of character positions already used, in the field into which the address string is to be inserted
Throws:
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:
  1. the session property mail.from
  2. the session properties mail.user or user.name, and mail.host
  3. the system property user.name and the hostname of localhost as determined by InetAddress.getLocalHost

Parameters:
session - the session

parse

public static InternetAddress[] parse(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(String addresslist,
                                      boolean strict)
                               throws AddressException
Parses the given comma-separated sequence of RFC 822 addresses into InternetAddresses. If strict is false, simple email addresses separated by spaces are also allowed. If strict is true, many (but not all) of the RFC 822 syntax rules are enforced. Even if strict is true, addresses composed of simple names (with no "@domain" part) are allowed.

Parameters:
addresslist - the comma-separated addresses
strict - whether to enforce RFC 822 syntax
Throws:
AddressException - if the parse failed

parseHeader

public static InternetAddress[] parseHeader(String addresslist,
                                            boolean strict)
                                     throws AddressException
Parses the given comma-separated sequence of RFC 822 addresses into InternetAddresses. If strict is false, simple email addresses separated by spaces are also allowed. If strict is true, many (but not all) of the RFC 822 syntax rules are enforced.

Parameters:
addresslist - the comma-separated addresses
strict - whether to enforce RFC 822 syntax
Throws:
AddressException - if the parse failed
Since:
JavaMail 1.3


© Copyright 2003, 2004 The Free Software Foundation, All rights reserved