Java Reference
In-Depth Information
public
static
String
toString
(
Address
[]
addresses
,
int
used
)
throws
ClassCastException
The
InternetAddress.toString()
methods convert an array of
Address
objects into
a comma-separated list of addresses encoded in pure ASCII, possibly folded onto mul‐
tiple lines. The optional
used
argument gives the number of characters that will precede
this string in the header field, such as To: or Cc:, into which this string will be inserted.
This information lets
toString()
decide where it needs to break the lines. A
ClassCastException
is thrown if any of the
Address
objects in the array are not more
specifically
InternetAddress
objects.
Finally, the
getLocalAddress()
method checks several system properties (
mail.from
,
mail.user
,
mail.host
, and
user.name
) as well as
InetAddress.getLocalName()
to
determine the email address of the current user:
public
static
InternetAddress
getLocalAddress
(
Session
session
)
For example, this code fragment tries to use the user's own email address rather than
one hardcoded into the program as a string:
msg
.
setFrom
(
InternetAddress
.
getLocalAddress
());
However, there's no guarantee that any of these properties will necessarily give the user's
true address.
The NewsAddress Class
Perhaps a little surprisingly, with an appropriate service provider, the JavaMail API can
also access Usenet news. The API is mostly the same as for reading a POP or IMAP
mailbox. However, instead of using an
InternetAddress
, you use a
NewsAddress
:
public
class
NewsAddress
extends
Address
A
NewsAddress
object represents a Usenet newsgroup name, such as
comp.lang.java.machine
. It may include the hostname for the news server as well. The
state of a
NewsAddress
object is maintained by two protected fields:
protected
String
newsgroup
protected
String
host
The
newsgroup
field contains the name of the newsgroup—for example,
netscape.devs-
java
. The
host
field is either null or contains the hostname of the news server—for
example,
secnews.netscape.com
. Both of these fields are set in the constructor. There are
three overloaded constructors for
NewsAddress
objects:
public
NewsAddress
()
public
NewsAddress
(
String
newsgroup
)
public
NewsAddress
(
String
newsgroup
,
String
host
)
They are used exactly as you'd expect. For example: