Java Reference
In-Depth Information
If the message object is more specifically a
MimeMessage
object, there is also a
set
From()
method that takes a
String
as an argument, so you don't have to first construct
an
Address
object:
public
void
setFrom
(
String
address
)
throws
MessagingException
,
IllegalWriteException
,
IllegalStateException
The Reply-to Address
Some messages contain a Reply-to: header indicating that any replies should be sent to
a different address than the one that sent the message. There are two methods to set and
get these addresses:
public
Address
[]
getReplyTo
()
throws
MessagingException
public
void
setReplyTo
(
Address
[]
addresses
)
throws
MessagingException
,
MethodNotSupportedException
,
IllegalWriteException
,
IllegalStateException
The semantics of these methods are the same as for the equivalent
getFrom()
and
setFrom()
methods—in fact, the default implementation of
getReplyTo()
simply re‐
turns
getFrom()
—with the single caveat that an implementation that doesn't support
separate Reply-to: addresses may throw a
MethodNotSupportedException
when
setReplyTo()
is invoked.
The Recipient Addresses
Whereas the sender of the message is generally found only in the From: header, the
recipients of the message are often split across the To:, Cc:, and Bcc: fields. Rather than
providing separate methods for each of these fields, the various
getRecipients()
and
setRecipients()
methods rely on a
Message.RecipientType
argument to determine
which field's value is desired.
RecipientType
is a public inner class in
jav
ax.mail.Message
whose private constructor limits it to exactly these three static objects:
Message
.
RecipientType
.
TO
Message
.
RecipientType
.
CC
Message
.
RecipientType
.
BCC
There are two methods to find the addressees of the
Message
:
public
abstract
Address
[]
getRecipients
(
Message
.
RecipientType
type
)
throws
MessagingException
public
Address
[]
getAllRecipients
()
throws
MessagingException
The
getRecipients()
method returns an array of
Address
objects, one for each address
listed in the specified header. It returns
null
if the specified header isn't present in the
message. It throws a
MessagingException
if the specified header is malformed in some
way. The
getAllRecipients()
method does the same thing, except that it combines
the contents of the To:, Cc:, and Bcc: headers.