Java Reference
In-Depth Information
There are two methods to set the recipients of the message while replacing any previous
recipients and two methods to add recipients to the message:
public
abstract
void
setRecipients
(
Message
.
RecipientType
type
,
Address
[]
addresses
)
throws
MessagingException
,
IllegalWriteException
,
IllegalStateException
public
void
setRecipient
(
Message
.
RecipientType
type
,
Address
address
)
throws
MessagingException
,
IllegalWriteException
public
abstract
void
addRecipients
(
Message
.
RecipientType
type
,
Address
[]
addresses
)
throws
MessagingException
,
IllegalWriteException
,
IllegalStateException
public
void
addRecipient
(
Message
.
RecipientType
type
,
Address
address
)
throws
MessagingException
,
IllegalWriteException
All four of these methods can throw a
MessagingException
, typically because one of
the addresses isn't in the right format. They can also throw an
IllegalWriteExcep
tion
if the specified field of the given
Message
object cannot be changed or an
Ille
galStateException
if the entire
Message
object is read-only.
The Subject of the Message
Since the subject is simply a single string of text, it's easy to get and set with these two
methods:
public
abstract
String
getSubject
()
throws
MessagingException
public
abstract
void
setSubject
(
String
subject
)
throws
MessagingException
,
IllegalWriteException
,
IllegalStateException
As with earlier setter methods, the getter returns
null
if the subject field isn't present
in the message. The setter throws an
IllegalWriteException
if the program isn't al‐
lowed to set the value of the Subject: field and an
IllegalStateException
if the
program isn't allowed to change the message at all.
The Date of the Message
Messages also have sent and received dates. Three methods allow programs to access
these fields:
public
abstract
Date
getSentDate
()
throws
MessagingException
public
abstract
void
setSentDate
(
Date
date
)
throws
MessagingException
,
IllegalWriteException
,
IllegalStateException
public
abstract
Date
getReceivedDate
()
throws
MessagingException
The underlying implementation is responsible for converting the textual date format
found in a message header like “Wed, 20 Aug 2014 10:57:04 0700” to a
java.util.Date
object. As usual, a
MessagingException
indicates some problem with the format of the
underlying message, an
IllegalWriteException
indicates that the field cannot be
changed, and an
IllegalStateException
indicates that the entire message cannot be
changed.