Java Reference
In-Depth Information
Creating Messages
The
Message
class has three constructors:
protected
Message
()
protected
Message
(
Folder
folder
,
int
messageNumber
)
protected
Message
(
Session
session
)
Since all the constructors are protected, they are primarily for the use of subclasses such
as
MimeMessage
. If you're sending a message, you'll use one of the constructors in the
subclass instead. If you're reading messages, the
Folder
or
Session
you're reading from
will create the
Message
objects and pass them to you.
Replying to Messages
If you already have a
Message
object, one way to create a new
Message
object is to reply
to the existing one using the
reply()
method:
public
abstract
Message
reply
(
boolean
replyToAll
)
throws
MessagingException
This method creates a new
Message
object with the same subject prefixed with “Re:”,
and addressed to the sender of the original message. If
replyToAll
is
true
, the message
is addressed to all known recipients of the original message. The content of the message
is empty. If you want to quote the original message, you'll have to do that yourself.
Getting Messages from Folders
You've already seen that when you're reading email, the JavaMail API creates
Message
objects to represent the messages it finds on the server. The primary means of doing
this are the
getMessage()
and
getMessages()
methods in the
Folder
class:
public
abstract
Message
getMessage
(
int
messageNumber
)
throws
MessagingException
public
Message
[]
getMessages
(
int
start
,
int
end
)
throws
MessagingException
public
Message
[]
getMessages
(
int
[]
messageNumbers
)
throws
MessagingException
public
Message
[]
getMessages
()
throws
MessagingException
The first three methods require the caller to specify which messages it wants. The last
simply returns all messages in the folder. What's actually returned are stubs holding the
places of the actual messages. The text and headers of the message won't necessarily be
retrieved until some method of the
Message
class is invoked that requires this infor‐
mation.