| Constructor: |
public InternetAddress() {
}
|
public InternetAddress(String address) throws AddressException {
// use our address parsing utility routine to parse the string
InternetAddress a[] = parse(address, true);
// if we got back anything other than a single address, it's an error
if (a.length != 1)
throw new AddressException("Illegal address", address);
/*
* Now copy the contents of the single address we parsed
* into the current object, which will be returned from the
* constructor.
* XXX - this sure is a round-about way of getting this done.
*/
this.address = a[0].address;
this.personal = a[0].personal;
this.encodedPersonal = a[0].encodedPersonal;
}
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 - the address in RFC822 format
Throws:
AddressException - if the parse failed
- exception:
AddressException - if the parse failed
|
public InternetAddress(String address,
boolean strict) throws AddressException {
this(address);
if (strict)
checkAddress(this.address, true, true);
}
Parse the given string and create an InternetAddress.
If strict is false, the detailed syntax of the
address isn't checked. Parameters:
address - the address in RFC822 format
strict - enforce RFC822 syntax
Throws:
AddressException - if the parse failed
- exception:
AddressException - if the parse failed
- since:
JavaMail - 1.3
|
public InternetAddress(String address,
String personal) throws UnsupportedEncodingException {
this(address, personal, null);
}
Construct an InternetAddress 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
|
public InternetAddress(String address,
String personal,
String charset) throws UnsupportedEncodingException {
this.address = address;
setPersonal(personal, charset);
}
Construct an InternetAddress 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
charset - the MIME charset for the name
|
| Method from javax.mail.internet.InternetAddress Detail: |
public Object clone() {
InternetAddress a = null;
try {
a = (InternetAddress)super.clone();
} catch (CloneNotSupportedException e) {} // Won't happen
return a;
}
Return a copy of this InternetAddress object. |
public boolean equals(Object a) {
if (!(a instanceof InternetAddress))
return false;
String s = ((InternetAddress)a).getAddress();
if (s == address)
return true;
if (address != null && address.equalsIgnoreCase(s))
return true;
return false;
}
|
public String getAddress() {
return address;
}
|
public InternetAddress[] getGroup(boolean strict) throws AddressException {
Vector groups = null;
String addr = getAddress();
// groups are of the form "name:addr,addr,...;"
if (!addr.endsWith(";"))
return null;
int ix = addr.indexOf(':");
if (ix < 0)
return null;
// extract the list
String list = addr.substring(ix + 1, addr.length() - 1);
// parse it and return the individual addresses
return InternetAddress.parseHeader(list, strict);
}
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. |
public static InternetAddress getLocalAddress(Session session) {
String user=null, host=null, address=null;
try {
if (session == null) {
user = System.getProperty("user.name");
host = InetAddress.getLocalHost().getHostName();
} else {
address = session.getProperty("mail.from");
if (address == null) {
user = session.getProperty("mail.user");
if (user == null || user.length() == 0)
user = session.getProperty("user.name");
if (user == null || user.length() == 0)
user = System.getProperty("user.name");
host = session.getProperty("mail.host");
if (host == null || host.length() == 0) {
InetAddress me = InetAddress.getLocalHost();
if (me != null)
host = me.getHostName();
}
}
}
if (address == null && user != null && user.length() != 0 &&
host != null && host.length() != 0)
address = user + "@" + host;
if (address != null)
return new InternetAddress(address);
} catch (SecurityException sex) { // ignore it
} catch (AddressException ex) { // ignore it
} catch (UnknownHostException ex) { } // ignore it
return null;
}
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. |
public String getPersonal() {
if (personal != null)
return personal;
if (encodedPersonal != null) {
try {
personal = MimeUtility.decodeText(encodedPersonal);
return personal;
} catch (Exception ex) {
// 1. ParseException: either its an unencoded string or
// it can't be parsed
// 2. UnsupportedEncodingException: can't decode it.
return encodedPersonal;
}
}
// No personal or encodedPersonal, return null
return null;
}
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. |
public String getType() {
return "rfc822";
}
Return the type of this address. The type of an InternetAddress
is "rfc822". |
public int hashCode() {
if (address == null)
return 0;
else
return address.toLowerCase(Locale.ENGLISH).hashCode();
}
Compute a hash code for the address. |
public boolean isGroup() {
// quick and dirty check
return address != null &&
address.endsWith(";") && address.indexOf(':") > 0;
}
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. |
public static InternetAddress[] parse(String addresslist) throws AddressException {
return parse(addresslist, true);
}
Parse the given comma separated sequence of addresses into
InternetAddress objects. Addresses must follow RFC822 syntax. |
public static InternetAddress[] parse(String addresslist,
boolean strict) throws AddressException {
return parse(addresslist, strict, false);
}
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. |
public static InternetAddress[] parseHeader(String addresslist,
boolean strict) throws AddressException {
return parse(addresslist, strict, true);
}
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. |
public void setAddress(String address) {
this.address = address;
}
|
public void setPersonal(String name) throws UnsupportedEncodingException {
personal = name;
if (name != null)
encodedPersonal = MimeUtility.encodeWord(name);
else
encodedPersonal = null;
}
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. |
public void setPersonal(String name,
String charset) throws UnsupportedEncodingException {
personal = name;
if (name != null)
encodedPersonal = MimeUtility.encodeWord(name, charset, null);
else
encodedPersonal = null;
}
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. |
public String toString() {
if (encodedPersonal == null && personal != null)
try {
encodedPersonal = MimeUtility.encodeWord(personal);
} catch (UnsupportedEncodingException ex) { }
if (encodedPersonal != null)
return quotePhrase(encodedPersonal) + " < " + address + " >";
else if (isGroup() || isSimple())
return address;
else
return "< " + address + " >";
}
Convert this address into a RFC 822 / RFC 2047 encoded address.
The resulting string contains only US-ASCII characters, and
hence is mail-safe. |
public static String toString(Address[] addresses) {
return toString(addresses, 0);
}
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. |
public static String toString(Address[] addresses,
int used) {
if (addresses == null || addresses.length == 0)
return null;
StringBuffer sb = new StringBuffer();
for (int i = 0; i < addresses.length; i++) {
if (i != 0) { // need to append comma
sb.append(", ");
used += 2;
}
String s = addresses[i].toString();
int len = lengthOfFirstSegment(s); // length till CRLF
if (used + len > 76) { // overflows ...
sb.append("\r\n\t"); // .. start new continuation line
used = 8; // account for the starting < tab > char
}
sb.append(s);
used = lengthOfLastSegment(s, used);
}
return sb.toString();
}
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. |
public String toUnicodeString() {
String p = getPersonal();
if (p != null)
return quotePhrase(p) + " < " + address + " >";
else if (isGroup() || isSimple())
return address;
else
return "< " + address + " >";
}
Returns a properly formatted address (RFC 822 syntax) of
Unicode characters. |
public void validate() throws AddressException {
checkAddress(getAddress(), true, true);
}
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. |