|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--javax.mail.Message | +--javax.mail.internet.MimeMessage
This class represents a MIME style email message. It implements
the Message
abstract class and the MimePart
interface.
Clients wanting to create new MIME style messages will instantiate an empty MimeMessage object and then fill it with appropriate attributes and content.
Service providers that implement MIME compliant backend stores may want to subclass MimeMessage and override certain methods to provide specific implementations. The simplest case is probably a provider that generates a MIME style input stream and leaves the parsing of the stream to this class.
MimeMessage uses the InternetHeaders
class to parse and
store the toplevel RFC 822 headers of a message.
RFC 822 header fields must contain only US-ASCII characters. MIME allows non ASCII characters to be present in certain portions of certain headers, by encoding those characters. RFC 2047 specifies the rules for doing this. The MimeUtility class provided in this package can be used to to achieve this.
MimeUtility
,
Part
,
Message
,
MimePart
Inner Class Summary | |
static class |
MimeMessage.RecipientType
This inner class extends the javax.mail.Message.RecipientType class to add additional RecipientTypes. |
Inner classes inherited from class javax.mail.Message |
Message.RecipientType |
Field Summary | |
protected byte[] |
content
Byte array that holds the bytes of this Message's content. |
protected javax.activation.DataHandler |
dh
The DataHandler object representing this Message's content. |
protected Flags |
flags
The Flags for this message. |
protected InternetHeaders |
headers
The InternetHeaders object that stores the header of this message. |
Fields inherited from class javax.mail.Message |
expunged,
folder,
msgnum,
session |
Constructor Summary | |
protected |
MimeMessage(Folder folder,
java.io.InputStream is,
int msgnum)
Constructs a MimeMessage by reading and parsing the data from the specified MIME InputStream. |
protected |
MimeMessage(Folder folder,
int msgnum)
Constructs an empty MimeMessage object with the given Folder and message number. |
protected |
MimeMessage(Folder folder,
InternetHeaders headers,
byte[] content,
int msgnum)
Constructs a MimeMessage from the given InternetHeaders object and content. |
|
MimeMessage(Session session)
Default constructor. |
|
MimeMessage(Session session,
java.io.InputStream is)
Constructs a MimeMessage by reading and parsing the data from the specified MIME InputStream. |
Method Summary | |
void |
addFrom(Address[] addresses)
Add the specified addresses to the existing "From" field. |
void |
addHeader(java.lang.String name,
java.lang.String value)
Add this value to the existing values for this header_name. |
void |
addHeaderLine(java.lang.String line)
Add a raw RFC 822 header-line. |
void |
addRecipients(Message.RecipientType type,
Address[] addresses)
Add the given addresses to the specified recipient type. |
java.util.Enumeration |
getAllHeaderLines()
Get all header lines as an Enumeration of Strings. |
java.util.Enumeration |
getAllHeaders()
Return all the headers from this Message as an enumeration of Header objects. |
Address[] |
getAllRecipients()
Get all the recipient addresses for the message. |
java.lang.Object |
getContent()
Return the content as a Java object. |
java.lang.String |
getContentID()
Returns the value of the "Content-ID" header field. |
java.lang.String[] |
getContentLanguage()
Get the languages specified in the "Content-Language" header field of this message. |
java.lang.String |
getContentMD5()
Return the value of the "Content-MD5" header field. |
protected java.io.InputStream |
getContentStream()
Produce the raw bytes of the content. |
java.lang.String |
getContentType()
Returns the value of the RFC 822 "Content-Type" header field. |
javax.activation.DataHandler |
getDataHandler()
Return a DataHandler for this Message's content. |
java.lang.String |
getDescription()
Returns the "Content-Description" header field of this Message. |
java.lang.String |
getDisposition()
Returns the value of the "Content-Disposition" header field. |
java.lang.String |
getEncoding()
Returns the content transfer encoding from the "Content-Transfer-Encoding" header field. |
java.lang.String |
getFileName()
Get the filename associated with this Message. |
Flags |
getFlags()
Return a Flags object containing the flags for
this message. |
Address[] |
getFrom()
Returns the value of the RFC 822 "From" header fields. |
java.lang.String[] |
getHeader(java.lang.String name)
Get all the headers for this header_name. |
java.lang.String |
getHeader(java.lang.String name,
java.lang.String delimiter)
Get all the headers for this header name, returned as a single String, with headers separated by the delimiter. |
java.io.InputStream |
getInputStream()
Return a decoded input stream for this Message's "content". |
int |
getLineCount()
Return the number of lines for the content of this message. |
java.util.Enumeration |
getMatchingHeaderLines(java.lang.String[] names)
Get matching header lines as an Enumeration of Strings. |
java.util.Enumeration |
getMatchingHeaders(java.lang.String[] names)
Return matching headers from this Message as an Enumeration of Header objects. |
java.lang.String |
getMessageID()
Returns the value of the "Message-ID" header field. |
java.util.Enumeration |
getNonMatchingHeaderLines(java.lang.String[] names)
Get non-matching header lines as an Enumeration of Strings. |
java.util.Enumeration |
getNonMatchingHeaders(java.lang.String[] names)
Return non-matching headers from this Message as an Enumeration of Header objects. |
java.util.Date |
getReceivedDate()
Returns the Date on this message was received. |
Address[] |
getRecipients(Message.RecipientType type)
Returns the recepients specified by the type. |
Address[] |
getReplyTo()
Return the value of the RFC 822 "Reply-To" header field. |
java.util.Date |
getSentDate()
Returns the value of the RFC 822 "Date" field. |
int |
getSize()
Return the size of the content of this message in bytes. |
java.lang.String |
getSubject()
Returns the value of the "Subject" header field. |
boolean |
isMimeType(java.lang.String mimeType)
Is this Part of the specified MIME type? This method compares only the primaryType and
subType . |
boolean |
isSet(Flags.Flag flag)
Check whether the flag specified in the flag
argument is set in this message. |
void |
removeHeader(java.lang.String name)
Remove all headers with this name. |
Message |
reply(boolean replyToAll)
Get a new Message suitable for a reply to this message. |
void |
saveChanges()
Updates the appropriate header fields of this message to be consistent with the message's contents. |
void |
setContent(Multipart mp)
This method sets the Message's content to a Multipart object. |
void |
setContent(java.lang.Object o,
java.lang.String type)
A convenience method for setting this Message's content. |
void |
setContentID(java.lang.String cid)
Set the "Content-ID" header field of this Message. |
void |
setContentLanguage(java.lang.String[] languages)
Set the "Content-Language" header of this MimePart. |
void |
setContentMD5(java.lang.String md5)
Set the "Content-MD5" header field of this Message. |
void |
setDataHandler(javax.activation.DataHandler dh)
This method provides the mechanism to set this part's content. |
void |
setDescription(java.lang.String description)
Set the "Content-Description" header field for this Message. |
void |
setDescription(java.lang.String description,
java.lang.String charset)
Set the "Content-Description" header field for this Message. |
void |
setDisposition(java.lang.String disposition)
Set the "Content-Disposition" header field of this Message. |
void |
setFileName(java.lang.String filename)
Set the filename associated with this part, if possible. |
void |
setFlags(Flags flag,
boolean set)
Set the flags for this message. |
void |
setFrom()
Set the RFC 822 "From" header field using the value of the InternetAddress.getLocalAddress method. |
void |
setFrom(Address address)
Set the RFC 822 "From" header field. |
void |
setHeader(java.lang.String name,
java.lang.String value)
Set the value for this header_name. |
void |
setRecipients(Message.RecipientType type,
Address[] addresses)
Set the specified recipient type to the given addresses. |
void |
setReplyTo(Address[] addresses)
Set the RFC 822 "Reply-To" header field. |
void |
setSentDate(java.util.Date d)
Set the RFC 822 "Date" header field. |
void |
setSubject(java.lang.String subject)
Set the "Subject" header field. |
void |
setSubject(java.lang.String subject,
java.lang.String charset)
Set the "Subject" header field. |
void |
setText(java.lang.String text)
Convenience method that sets the given String as this part's content, with a MIME type of "text/plain". |
void |
setText(java.lang.String text,
java.lang.String charset)
Convenience method that sets the given String as this part's content, with a MIME type of "text/plain" and the specified charset. |
protected void |
updateHeaders()
Called by the saveChanges method to actually
update the MIME headers. |
void |
writeTo(java.io.OutputStream os)
Output the message as an RFC 822 format stream. |
void |
writeTo(java.io.OutputStream os,
java.lang.String[] ignoreList)
Output the message as an RFC 822 format stream, without specified headers. |
Methods inherited from class javax.mail.Message |
addRecipient,
getFolder,
getMessageNumber,
isExpunged,
match,
setExpunged,
setFlag,
setMessageNumber,
setRecipient |
Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait |
Field Detail |
protected javax.activation.DataHandler dh
protected byte[] content
protected InternetHeaders headers
protected Flags flags
Constructor Detail |
public MimeMessage(Session session)
headers
field is set to an empty InternetHeaders
object. The flags
field is set to an empty Flags
object.public MimeMessage(Session session, java.io.InputStream is) throws MessagingException
session
- Session object for this messageis
- the message input streamprotected MimeMessage(Folder folder, int msgnum)
This method is for providers subclassing MimeMessage
.
protected MimeMessage(Folder folder, java.io.InputStream is, int msgnum) throws MessagingException
This method is for providers subclassing MimeMessage
.
folder
- The containing folder.is
- the message input streammsgnum
- Message number of this message within its folderprotected MimeMessage(Folder folder, InternetHeaders headers, byte[] content, int msgnum) throws MessagingException
MimeMessage
.folder
- The containing folder.is
- the message input streammsgnum
- Message number of this message within its folderMethod Detail |
public Address[] getFrom() throws MessagingException
null
is returned.
This implementation uses the getHeader
method
to obtain the requisite header field.
headers
public void setFrom(Address address) throws MessagingException
null
,
this header is removed.address
- the sender of this messagepublic void setFrom() throws MessagingException
InternetAddress.getLocalAddress
method.public void addFrom(Address[] addresses) throws MessagingException
addresses
- the senders of this messagepublic Address[] getRecipients(Message.RecipientType type) throws MessagingException
Message.RecipientType.TO "To" Message.RecipientType.CC "Cc" Message.RecipientType.BCC "Bcc" MimeMessage.RecipientType.NEWSGROUPS "Newsgroups"
This implementation uses the getHeader
method
to obtain the requisite header field.
Type
- of recepientheaders
,
Message.RecipientType.TO
,
Message.RecipientType.CC
,
Message.RecipientType.BCC
,
MimeMessage.RecipientType.NEWSGROUPS
public Address[] getAllRecipients() throws MessagingException
Message.RecipientType.TO
,
Message.RecipientType.CC
,
Message.RecipientType.BCC
,
MimeMessage.RecipientType.NEWSGROUPS
public void setRecipients(Message.RecipientType type, Address[] addresses) throws MessagingException
null
, the corresponding
recipient field is removed.type
- Recipient typeaddresses
- AddressesgetRecipients(javax.mail.Message.RecipientType)
public void addRecipients(Message.RecipientType type, Address[] addresses) throws MessagingException
type
- Recipient typeaddresses
- Addressespublic Address[] getReplyTo() throws MessagingException
getFrom
method is called and its value is returned.
This implementation uses the getHeader
method
to obtain the requisite header field.headers
public void setReplyTo(Address[] addresses) throws MessagingException
null
, this header is removed.public java.lang.String getSubject() throws MessagingException
If the subject is encoded as per RFC 2047, it is decoded and converted into Unicode. If the decoding or conversion fails, the raw data is returned as is.
This implementation uses the getHeader
method
to obtain the requisite header field.
headers
public void setSubject(java.lang.String subject) throws MessagingException
Note that if the charset encoding process fails, a MessagingException is thrown, and an UnsupportedEncodingException is included in the chain of nested exceptions within the MessagingException.
subject
- The subjectpublic void setSubject(java.lang.String subject, java.lang.String charset) throws MessagingException
Note that if the charset encoding process fails, a MessagingException is thrown, and an UnsupportedEncodingException is included in the chain of nested exceptions within the MessagingException.
subject
- The subjectcharset
- The charsetpublic java.util.Date getSentDate() throws MessagingException
This implementation uses the getHeader
method
to obtain the requisite header field.
public void setSentDate(java.util.Date d) throws MessagingException
null
, the existing "Date" field is removed.public java.util.Date getReceivedDate() throws MessagingException
null
if this date cannot be obtained. Note that RFC 822 does not define a field for the received date. Hence only implementations that can provide this date need return a valid value.
This implementation returns null
.
public int getSize() throws MessagingException
Note that this number may not be an exact measure of the content size and may or may not account for any transfer encoding of the content.
This implementation returns the size of the content byte array, or -1 if there is no content byte array.
public int getLineCount() throws MessagingException
Note that this number may not be an exact measure of the content length and may or may not account for any transfer encoding of the content.
This implementation returns -1.
public java.lang.String getContentType() throws MessagingException
This implementation uses the getHeader
method
to obtain the requisite header field.
DataHandler
public boolean isMimeType(java.lang.String mimeType) throws MessagingException
primaryType
and
subType
.
The parameters of the content types are ignored.
For example, this method will return true
when
comparing a Part of content type "text/plain"
with "text/plain; charset=foobar".
If the subType
of mimeType
is the
special character '*', then the subtype is ignored during the
comparison.
public java.lang.String getDisposition() throws MessagingException
If the Content-Disposition field is unavailable,
null
is returned.
This implementation uses the getHeader
method
to obtain the requisite header field.
public void setDisposition(java.lang.String disposition) throws MessagingException
disposition
is null, any existing "Content-Disposition"
header field is removed.public java.lang.String getEncoding() throws MessagingException
null
if the header is unavailable
or its value is absent.
This implementation uses the getHeader
method
to obtain the requisite header field.
public java.lang.String getContentID() throws MessagingException
null
if the field is unavailable or its value is
absent.
This implementation uses the getHeader
method
to obtain the requisite header field.
public void setContentID(java.lang.String cid) throws MessagingException
cid
parameter is null, any existing
"Content-ID" is removed.public java.lang.String getContentMD5() throws MessagingException
null
if this field is unavailable or its value
is absent.
This implementation uses the getHeader
method
to obtain the requisite header field.
public void setContentMD5(java.lang.String md5) throws MessagingException
public java.lang.String getDescription() throws MessagingException
If the Content-Description field is encoded as per RFC 2047, it is decoded and converted into Unicode. If the decoding or conversion fails, the raw data is returned as-is
This implementation uses the getHeader
method
to obtain the requisite header field.
public void setDescription(java.lang.String description) throws MessagingException
null
, then any
existing "Content-Description" fields are removed. If the description contains non US-ASCII characters, it will be encoded using the platform's default charset. If the description contains only US-ASCII characters, no encoding is done and it is used as-is.
Note that if the charset encoding process fails, a MessagingException is thrown, and an UnsupportedEncodingException is included in the chain of nested exceptions within the MessagingException.
description
- content-descriptionpublic void setDescription(java.lang.String description, java.lang.String charset) throws MessagingException
null
, then any
existing "Content-Description" fields are removed. If the description contains non US-ASCII characters, it will be encoded using the specified charset. If the description contains only US-ASCII characters, no encoding is done and it is used as-is.
Note that if the charset encoding process fails, a MessagingException is thrown, and an UnsupportedEncodingException is included in the chain of nested exceptions within the MessagingException.
description
- Descriptioncharset
- Charset for encodingpublic java.lang.String[] getContentLanguage() throws MessagingException
null
if this field is unavailable
or its value is absent.
This implementation uses the getHeader
method
to obtain the requisite header field.
public void setContentLanguage(java.lang.String[] languages) throws MessagingException
languages
- array of language tagspublic java.lang.String getMessageID() throws MessagingException
The default implementation provided here uses the
getHeader
method to return the value of the
"Message-ID" field.
MessageIDTerm
public java.lang.String getFileName() throws MessagingException
Returns the value of the "filename" parameter from the
"Content-Disposition" header field of this message. If it's
not available, returns the value of the "name" parameter from
the "Content-Type" header field of this BodyPart.
Returns null
if both are absent.
public void setFileName(java.lang.String filename) throws MessagingException
Sets the "filename" parameter of the "Content-Disposition" header field of this message.
public java.io.InputStream getInputStream() throws java.io.IOException, MessagingException
This implementation obtains the input stream from the DataHandler,
that is, it invokes getDataHandler().getInputStream()
.
getContentStream()
,
DataHandler.getInputStream()
protected java.io.InputStream getContentStream() throws MessagingException
This implementation just returns a ByteArrayInputStream constructed
out of the content
byte array.
content
public javax.activation.DataHandler getDataHandler() throws MessagingException
The implementation provided here works as follows. Note the use of
the getContentStream
method to
generate the byte stream for the content. Also note that
any transfer-decoding is done automatically within this method.
getDataHandler() { if (dh == null) { dh = new DataHandler(new MimePartDataSource(this)); } return dh; }class MimePartDataSource implements DataSource { public getInputStream() { return MimeUtility.decode( getContentStream(), getEncoding()); } ....
}
public java.lang.Object getContent() throws java.io.IOException, MessagingException
This implementation obtains the content from the DataHandler,
that is, it invokes getDataHandler().getContent()
.
Part
,
DataHandler.getContent()
public void setDataHandler(javax.activation.DataHandler dh) throws MessagingException
dh
- The DataHandler for the content.public void setContent(java.lang.Object o, java.lang.String type) throws MessagingException
The content is wrapped in a DataHandler object. Note that a
DataContentHandler class for the specified type should be
available to the JavaMail implementation for this to work right.
i.e., to do setContent(foobar, "application/x-foobar")
,
a DataContentHandler for "application/x-foobar" should be installed.
Refer to the Java Activation Framework for more information.
o
- the content objecttype
- Mime type of the objectpublic void setText(java.lang.String text) throws MessagingException
Note that there may be a performance penalty if
text
is large, since this method may have
to scan all the characters to determine what charset to
use.
If the charset is already known, use the
setText
method that takes the charset parameter.
setText(String text, String charset)
public void setText(java.lang.String text, java.lang.String charset) throws MessagingException
public void setContent(Multipart mp) throws MessagingException
mp
- The multipart object that is the Message's contentpublic Message reply(boolean replyToAll) throws MessagingException
If replyToAll
is set, the new Message will be addressed
to all recipients of this message. Otherwise, the reply will be
addressed to only the sender of this message (using the value
of the getReplyTo
method).
The "Subject" field is filled in with the original subject
prefixed with "Re:" (unless it already starts with "Re:").
The "In-Reply-To" header is set in the new message if this
message has a "Message-Id" header. The ANSWERED
flag is set in this message.
replyToAll
- reply should be sent to all recipients
of this messagepublic void writeTo(java.io.OutputStream os) throws java.io.IOException, MessagingException
Note that, depending on how the messag was constructed, it may use a variety of line termination conventions. Generally the output should be sent through an appropriate FilterOutputStream that converts the line terminators to the desired form, either CRLF for MIME compatibility and for use in Internet protocols, or the local platform's line terminator for storage in a local text file.
DataHandler.writeTo(java.io.OutputStream)
public void writeTo(java.io.OutputStream os, java.lang.String[] ignoreList) throws java.io.IOException, MessagingException
DataHandler.writeTo(java.io.OutputStream)
public java.lang.String[] getHeader(java.lang.String name) throws MessagingException
This implementation obtains the headers from the
headers
InternetHeaders object.
name
- name of headerMimeUtility
public java.lang.String getHeader(java.lang.String name, java.lang.String delimiter) throws MessagingException
null
, only the first header is
returned.header_name
- the name of this headerpublic void setHeader(java.lang.String name, java.lang.String value) throws MessagingException
name
- header namevalue
- header valueMimeUtility
public void addHeader(java.lang.String name, java.lang.String value) throws MessagingException
name
- header namevalue
- header valueMimeUtility
public void removeHeader(java.lang.String name) throws MessagingException
public java.util.Enumeration getAllHeaders() throws MessagingException
Note that certain headers may be encoded as per RFC 2047 if they contain non US-ASCII characters and these should be decoded.
This implementation obtains the headers from the
headers
InternetHeaders object.
MimeUtility
public java.util.Enumeration getMatchingHeaders(java.lang.String[] names) throws MessagingException
headers
InternetHeaders object.public java.util.Enumeration getNonMatchingHeaders(java.lang.String[] names) throws MessagingException
headers
InternetHeaders object.public void addHeaderLine(java.lang.String line) throws MessagingException
public java.util.Enumeration getAllHeaderLines() throws MessagingException
public java.util.Enumeration getMatchingHeaderLines(java.lang.String[] names) throws MessagingException
public java.util.Enumeration getNonMatchingHeaderLines(java.lang.String[] names) throws MessagingException
public Flags getFlags() throws MessagingException
Flags
object containing the flags for
this message. Note that a clone of the internal Flags object is returned, so modifying the returned Flags object will not affect the flags of this message.
Flags
public boolean isSet(Flags.Flag flag) throws MessagingException
flag
argument is set in this message.
This implementation checks this message's internal
flags
object.
flag
- the flagFlags.Flag
,
Flags.Flag.ANSWERED
,
Flags.Flag.DELETED
,
Flags.Flag.DRAFT
,
Flags.Flag.FLAGGED
,
Flags.Flag.RECENT
,
Flags.Flag.SEEN
public void setFlags(Flags flag, boolean set) throws MessagingException
This implementation modifies the flags
field.
public void saveChanges() throws MessagingException
If any part of a message's headers or contents are changed,
saveChanges
must be called to ensure that those
changes are permanent. Else, any such modifications may or
may not be saved, depending on the folder implementation.
Messages obtained from folders opened READ_ONLY should not be modified and saveChanges should not be called on such messages.
protected void updateHeaders() throws MessagingException
saveChanges
method to actually
update the MIME headers. The implementation here sets the
Content-Transfer-Encoding
header (if needed
and not already set), the Mime-Version
header
and the Message-ID
header. Also, if the content
of this message is a MimeMultipart
, it's
updateHeaders
method is called.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |