Java Reference
In-Depth Information
Basic Header Info
A typical RFC 822 message contains a header that looks something like this:
From levi@blazing.sunspot.noao.edu Tue Aug 5 10:57:08 2014
Date: Tue, 27 Aug 2014 10:57:04 +0700
From: levi@blazing.sunspot.noao.edu (Denise Levi)
To: volleyball@sunspot.noao.edu
Subject: Apologies
Content-Length: 517
Status: RO
X-Lines: 13
The exact fields in the header can vary, but most messages contain at least a From: field,
a To: field, a Date: field, and a Subject: field. Other common fields include Cc: (carbon
copies) and Bcc: (blind carbon copies). In general, these will be accessible through getter
and setter methods.
The From Address
These four methods get and set the From: field of a message:
public
abstract
Address
[]
getFrom
()
throws
MessagingException
public
abstract
void
setFrom
()
throws
MessagingException
,
IllegalWriteException
,
IllegalStateException
public
abstract
void
setFrom
(
Address
address
)
throws
MessagingException
,
IllegalWriteException
,
IllegalStateException
public
abstract
void
addFrom
(
Address
[]
addresses
)
throws
MessagingException
,
IllegalWriteException
,
IllegalStateException
The
getFrom()
method returns an array of
Address
objects, one for each address listed
in the From: header. (In practice, it's rare for a message to be
from
more than one address.
It's quite common for a message to be addressed
to
more than one address.) It returns
null
if the From: header isn't present in the message. It throws a
MessagingException
if the From: header is malformed in some way.
The noargs
setFrom()
and
addFrom()
methods set and modify the From: headers of
outgoing email messages. The noargs
setFrom()
method sets the header to the current
value of the
mail.user
property or, as a fallback, the
user.name
property. The
set
From()
method with arguments sets the value of the From: header to the listed addresses.
The
addFrom()
method adds the listed addresses to any addresses that already exist in
the header. All three of these methods can throw a
MessagingException
if one of the
addresses they use isn't in the right format. They can also throw an
Illegal
WriteException
if the From: field of the given
Message
object cannot be changed or an
IllegalStateException
if the entire
Message
object is read-only.