com.openexchange.mail.mime
Class QuotedInternetAddress

java.lang.Object
  extended by javax.mail.Address
      extended by javax.mail.internet.InternetAddress
          extended by com.openexchange.mail.mime.QuotedInternetAddress
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public final class QuotedInternetAddress
extends javax.mail.internet.InternetAddress

QuotedInternetAddress - A quoted version of InternetAddress originally written by Bill Shannon and John Mani. Moreover this class supports punycode.

Quotes are added to encoded personal names to maintain them when converting to mail-safe version. Parental InternetAddress class ignores quotes when when converting to mail-safe version:

``"Müller, Jan" <mj@foo.de>''
is converted to
``=?UTF-8?Q?M=C3=BCller=2C_Jan?= <mj@foo.de>''

This class maintains the quotes in mail-safe version:

``"Müller, Jan" <mj@foo.de>''
is converted to
``=?UTF-8?Q?=22M=C3=BCller=2C_Jan=22?= <mj@foo.de>''

Author:
Thorben Betten
See Also:
Serialized Form

Constructor Summary
QuotedInternetAddress()
          Initializes a new QuotedInternetAddress.
QuotedInternetAddress(java.lang.String address)
          Initializes a new QuotedInternetAddress.
QuotedInternetAddress(java.lang.String address, boolean strict)
          Initializes a new QuotedInternetAddress.
QuotedInternetAddress(java.lang.String address, java.lang.String personal)
          Initializes a new QuotedInternetAddress.
QuotedInternetAddress(java.lang.String address, java.lang.String personal, java.lang.String charset)
          Initializes a new QuotedInternetAddress.
 
Method Summary
 java.lang.String getUnicodeAddress()
          Gets the email address in Unicode characters.
static javax.mail.internet.InternetAddress[] parse(java.lang.String addresslist)
          Parse the given comma separated sequence of addresses into InternetAddress objects.
static javax.mail.internet.InternetAddress[] parse(java.lang.String addresslist, boolean strict)
          Parse the given sequence of addresses into InternetAddress objects.
 void parseAddress(java.lang.String address)
          Parses the given string into this QuotedInternetAddress.
static javax.mail.internet.InternetAddress[] parseHeader(java.lang.String addresslist, boolean strict)
          Parse the given sequence of addresses into InternetAddress objects.
 void setAddress(java.lang.String address)
          Sets the email address.
static java.lang.String toACE(java.lang.String idnAddress)
          Converts a unicode representation of an internet address to ASCII using the procedure in RFC3490 section 4.1.
static java.lang.String toIDN(java.lang.String aceAddress)
          Converts an ASCII-encoded address to its unicode representation.
static javax.mail.internet.InternetAddress[] toQuotedAddresses(javax.mail.internet.InternetAddress[] addrs)
          Converts given array of InternetAddress to quoted addresses
 java.lang.String toString()
          Convert this address into a RFC 822 / RFC 2047 encoded address.
 java.lang.String toUnicodeString()
          Returns a properly formatted address (RFC 822 syntax) of Unicode characters.
 
Methods inherited from class javax.mail.internet.InternetAddress
clone, equals, getAddress, getGroup, getLocalAddress, getPersonal, getType, hashCode, isGroup, setPersonal, setPersonal, toString, toString, validate
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

QuotedInternetAddress

public QuotedInternetAddress()
Initializes a new QuotedInternetAddress.


QuotedInternetAddress

public QuotedInternetAddress(java.lang.String address)
                      throws javax.mail.internet.AddressException
Initializes a new QuotedInternetAddress.

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 - The address in RFC822 format
Throws:
javax.mail.internet.AddressException - If parsing the address fails

QuotedInternetAddress

public QuotedInternetAddress(java.lang.String address,
                             boolean strict)
                      throws javax.mail.internet.AddressException
Initializes a new QuotedInternetAddress.

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

Parameters:
address - The address in RFC822 format
strict - true enforce RFC822 syntax; otherwise false
Throws:
javax.mail.internet.AddressException - If parsing the address fails

QuotedInternetAddress

public QuotedInternetAddress(java.lang.String address,
                             java.lang.String personal)
                      throws javax.mail.internet.AddressException,
                             java.io.UnsupportedEncodingException
Initializes a new QuotedInternetAddress.

Construct an instance 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:
javax.mail.internet.AddressException - If parsing the address fails
java.io.UnsupportedEncodingException - If encoding is not supported

QuotedInternetAddress

public QuotedInternetAddress(java.lang.String address,
                             java.lang.String personal,
                             java.lang.String charset)
                      throws javax.mail.internet.AddressException,
                             java.io.UnsupportedEncodingException
Initializes a new QuotedInternetAddress.

Parameters:
address - The address in RFC822 format
personal - The personal name
charset - The MIME charset for the name
Throws:
javax.mail.internet.AddressException - If parsing the address fails
java.io.UnsupportedEncodingException - If encoding is not supported
Method Detail

toQuotedAddresses

public static javax.mail.internet.InternetAddress[] toQuotedAddresses(javax.mail.internet.InternetAddress[] addrs)
                                                               throws javax.mail.internet.AddressException
Converts given array of InternetAddress to quoted addresses

Parameters:
addrs - The addresses to convert
Returns:
The quoted addresses
Throws:
javax.mail.internet.AddressException - If conversion fails

parse

public static javax.mail.internet.InternetAddress[] parse(java.lang.String addresslist)
                                                   throws javax.mail.internet.AddressException
Parse the given comma separated sequence of addresses into InternetAddress objects. Addresses must follow RFC822 syntax.

Parameters:
addresslist - A comma separated address strings
Returns:
An array of InternetAddress objects
Throws:
javax.mail.internet.AddressException - If the parse failed

parse

public static javax.mail.internet.InternetAddress[] parse(java.lang.String addresslist,
                                                          boolean strict)
                                                   throws javax.mail.internet.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 - A comma separated address strings
strict - true to enforce RFC822 syntax; otherwise false
Returns:
An array of InternetAddress objects
Throws:
javax.mail.internet.AddressException - If the parse failed

parseHeader

public static javax.mail.internet.InternetAddress[] parseHeader(java.lang.String addresslist,
                                                                boolean strict)
                                                         throws javax.mail.internet.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 - A comma separated address strings
strict - true to enforce RFC822 syntax; otherwise false
Returns:
An array of InternetAddress objects
Throws:
javax.mail.internet.AddressException - If the parse failed

toACE

public static java.lang.String toACE(java.lang.String idnAddress)
                              throws javax.mail.internet.AddressException
Converts a unicode representation of an internet address to ASCII using the procedure in RFC3490 section 4.1. Unassigned characters are not allowed and STD3 ASCII rules are enforced.

This implementation already supports EsZett character. Thanks to http.net!

"someone@müller.de" is converted to "someone@xn--mller-kva.de"

Parameters:
idnAddress - The unicode representation of an internet address
Returns:
The ASCII-encoded (punycode) of given internet address
Throws:
javax.mail.internet.AddressException - If ASCII representation of given internet address cannot be created

toIDN

public static java.lang.String toIDN(java.lang.String aceAddress)
Converts an ASCII-encoded address to its unicode representation. Unassigned characters are not allowed and STD3 hostnames are enforced.

This implementation already supports EsZett character. Thanks to http.net!

"someone@xn--mller-kva.de" is converted to "someone@müller.de"

Parameters:
aceAddress - The ASCII-encoded (punycode) address
Returns:
The unicode representation of given internet address

parseAddress

public void parseAddress(java.lang.String address)
                  throws javax.mail.internet.AddressException
Parses the given string into this QuotedInternetAddress.

Parameters:
address - The address in RFC822 format
Throws:
javax.mail.internet.AddressException - If parsing the address fails

setAddress

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

Overrides:
setAddress in class javax.mail.internet.InternetAddress
Parameters:
address - The email address

getUnicodeAddress

public java.lang.String getUnicodeAddress()
Gets the email address in Unicode characters.

Returns:
The email address in Unicode characters

toString

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

Overrides:
toString in class javax.mail.internet.InternetAddress
Returns:
possibly encoded address string

toUnicodeString

public java.lang.String toUnicodeString()
Returns a properly formatted address (RFC 822 syntax) of Unicode characters.

Overrides:
toUnicodeString in class javax.mail.internet.InternetAddress
Returns:
The Unicode address string