Top Description Inners Fields Constructors Methods
javax.mail.internet

public Class MimeMessage

extends Message
implements MimePart
Class Inheritance
All Implemented Interfaces
javax.mail.internet.MimePart, javax.mail.Part
Known Direct Subclasses
com.sun.mail.imap.IMAPMessage, com.sun.mail.pop3.POP3Message, com.sun.mail.smtp.SMTPMessage
Imports
javax.mail.*, javax.activation.*, java.lang.*, java.io.*, java.util.*, java.text.ParseException, com.sun.mail.util.PropUtil, .ASCIIUtility, .MimeUtil, .MessageRemovedIOException, .FolderClosedIOException, .LineOutputStream, javax.mail.util.SharedByteArrayInputStream

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 top level RFC 822 headers of a message.

The mail.mime.address.strict session property controls the parsing of address headers. By default, strict parsing of address headers is done. If this property is set to "false", strict parsing is not done and many illegal addresses that sometimes occur in real messages are allowed. See the InternetAddress class for details.


A note on RFC 822 and MIME headers

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. Callers of the setHeader, addHeader, and addHeaderLine methods are responsible for enforcing the MIME requirements for the specified headers. In addition, these header fields must be folded (wrapped) before being sent if they exceed the line length limitation for the transport (1000 bytes for SMTP). Received headers may have been folded. The application is responsible for folding and unfolding headers as appropriate.

Authors
John Mani, Bill Shannon, Max Spivak, Kanwar Oberoi
See Also
javax.mail.internet.MimeUtility, javax.mail.Part, javax.mail.Message, javax.mail.internet.MimePart, javax.mail.internet.InternetAddress

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
MimeMessage.RecipientType

This inner class extends the javax.mail.Message.RecipientType class to add additional RecipientTypes.

Field Summary

Modifier and TypeField and Description
private boolean
private static final Flags
protected Object
cachedContent

If our content is a Multipart or Message object, we save it the first time it's created by parsing a stream so that changes to the contained objects will not be lost.

protected byte[]
content

Byte array that holds the bytes of this Message's content.

protected InputStream
contentStream

If the data for this message was supplied by an InputStream that implements the SharedInputStream interface, contentStream is another such stream representing the content of this message.

protected 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.

private static final MailDateFormat
protected boolean
modified

A flag indicating whether the message has been modified.

protected boolean
saved

Does the saveChanges method need to be called on this message?

private boolean
Inherited from javax.mail.Message:
expungedfoldermsgnumsession

Constructor Summary

AccessConstructor and Description
public
MimeMessage(Session
the Sesssion
session
)

Default constructor.

public
MimeMessage(Session
Session object for this message
session
,
InputStream
the message input stream
is
)

Constructs a MimeMessage by reading and parsing the data from the specified MIME InputStream.

public
MimeMessage(MimeMessage
the message to copy content from
source
)

Constructs a new MimeMessage with content initialized from the source MimeMessage.

protected
MimeMessage(Folder
the Folder this message is from
folder
,
int
the number of this message
msgnum
)

Constructs an empty MimeMessage object with the given Folder and message number.

protected
MimeMessage(Folder
The containing folder.
folder
,
InputStream
the message input stream
is
,
int
Message number of this message within its folder
msgnum
)

Constructs a MimeMessage by reading and parsing the data from the specified MIME InputStream.

protected
MimeMessage(Folder
The containing folder.
folder
,
InternetHeaders
The headers
headers
,
byte[]
The message content
content
,
int
Message number of this message within its folder
msgnum
)

Constructs a MimeMessage from the given InternetHeaders object and content.

Method Summary

Modifier and TypeMethod and Description
private void
addAddressHeader(String name, Address[] addresses)

public void
addFrom(Address[]
the senders of this message
addresses
)

Implements abstract javax.mail.Message.addFrom.

Add the specified addresses to the existing "From" field.
public void
addHeader(String
header name
name
,
String
header value
value
)

Implements javax.mail.Part.addHeader.

Add this value to the existing values for this header_name.
public void
addHeaderLine(String
the line to add
line
)

Implements javax.mail.internet.MimePart.addHeaderLine.

Add a raw RFC 822 header-line.
public void
addRecipients(Message.RecipientType
Recipient type
type
,
Address[]
Addresses
addresses
)

Implements abstract javax.mail.Message.addRecipients.

Add the given addresses to the specified recipient type.
public void
addRecipients(Message.RecipientType
Recipient type
type
,
String
Addresses
addresses
)

Add the given addresses to the specified recipient type.

protected InternetHeaders

Returns:

an InternetHeaders object
createInternetHeaders
(InputStream
the InputStream to read the headers from
is
)

Create and return an InternetHeaders object that loads the headers from the given InputStream.

protected MimeMessage

Returns:

the new MimeMessage object
createMimeMessage
(Session
the Session to use for the new message
session
)

Create and return a MimeMessage object.

private Address[]
eliminateDuplicates(List<Address> v, Address[] addrs)

Check addrs for any duplicates that may already be in v. Return a new array without the duplicates.

private Address[]
public Enumeration<String>
getAllHeaderLines()

Implements javax.mail.internet.MimePart.getAllHeaderLines.

Get all header lines as an Enumeration of Strings.
public Enumeration<Header>

Returns:

array of header objects
getAllHeaders
()

Implements javax.mail.Part.getAllHeaders.

Return all the headers from this Message as an enumeration of Header objects.
public Address[]

Returns:

array of Address objects
getAllRecipients
()

Overrides javax.mail.Message.getAllRecipients.

Get all the recipient addresses for the message.
public Object

Returns:

Object
getContent
()

Implements javax.mail.Part.getContent.

Return the content as a Java object.
public String

Returns:

content-ID
getContentID
()

Implements javax.mail.internet.MimePart.getContentID.

Returns the value of the "Content-ID" header field.
public String[]

Returns:

value of content-language header.
getContentLanguage
()

Implements javax.mail.internet.MimePart.getContentLanguage.

Get the languages specified in the "Content-Language" header field of this message.
public String

Returns:

content-MD5
getContentMD5
()

Implements javax.mail.internet.MimePart.getContentMD5.

Return the value of the "Content-MD5" header field.
protected InputStream

Returns:

an InputStream containing the raw bytes
getContentStream
()

Produce the raw bytes of the content.

public String

Returns:

The ContentType of this part
getContentType
()

Implements javax.mail.Part.getContentType.

Returns the value of the RFC 822 "Content-Type" header field.
public synchronized DataHandler
getDataHandler()

Implements javax.mail.Part.getDataHandler.

Return a DataHandler for this Message's content.
public String

Returns:

content-description
getDescription
()

Implements javax.mail.Part.getDescription.

Returns the "Content-Description" header field of this Message.
public String

Returns:

disposition of this part, or null if unknown
getDisposition
()

Implements javax.mail.Part.getDisposition.

Returns the disposition from the "Content-Disposition" header field.
public String

Returns:

content-transfer-encoding
getEncoding
()

Implements javax.mail.internet.MimePart.getEncoding.

Returns the content transfer encoding from the "Content-Transfer-Encoding" header field.
public String

Returns:

filename
getFileName
()

Implements javax.mail.Part.getFileName.

Get the filename associated with this Message.
public synchronized Flags

Returns:

Flags object containing the flags for this message
getFlags
()

Implements abstract javax.mail.Message.getFlags.

Return a Flags object containing the flags for this message.
public Address[]

Returns:

Address object
getFrom
()

Implements abstract javax.mail.Message.getFrom.

Returns the value of the RFC 822 "From" header fields.
public String[]

Returns:

array of headers
getHeader
(String
name of header
name
)

Implements javax.mail.Part.getHeader.

Get all the headers for this header_name.
public String

Returns:

the value fields for all headers with this name
getHeader
(String
the name of this header
name
,
String
separator between values
delimiter
)

Implements javax.mail.internet.MimePart.getHeader.

Get all the headers for this header name, returned as a single String, with headers separated by the delimiter.
private String
public InputStream

Returns:

an InputStream
getInputStream
()

Implements javax.mail.Part.getInputStream.

Return a decoded input stream for this Message's "content".
public int

Returns:

number of lines in the content.
getLineCount
()

Implements javax.mail.Part.getLineCount.

Return the number of lines for the content of this message.
public Enumeration<String>
getMatchingHeaderLines(String[]
the headers to return
names
)

Implements javax.mail.internet.MimePart.getMatchingHeaderLines.

Get matching header lines as an Enumeration of Strings.
public Enumeration<Header>
getMatchingHeaders(String[]
the headers to match
names
)

Implements javax.mail.Part.getMatchingHeaders.

Return matching headers from this Message as an Enumeration of Header objects.
public String

Returns:

Message-ID
getMessageID
()

Returns the value of the "Message-ID" header field.

public Enumeration<String>
getNonMatchingHeaderLines(String[]
the headers to not return
names
)

Implements javax.mail.internet.MimePart.getNonMatchingHeaderLines.

Get non-matching header lines as an Enumeration of Strings.
public Enumeration<Header>
getNonMatchingHeaders(String[]
the headers to not match
names
)

Implements javax.mail.Part.getNonMatchingHeaders.

Return non-matching headers from this Message as an Enumeration of Header objects.
public InputStream

Returns:

an InputStream containing the raw bytes
getRawInputStream
()

Return an InputStream to the raw data with any Content-Transfer-Encoding intact.

public Date

Returns:

the date this message was received
getReceivedDate
()

Implements abstract javax.mail.Message.getReceivedDate.

Returns the Date on this message was received.
public Address[]

Returns:

array of Address objects
getRecipients
(Message.RecipientType
Type of recepient
type
)

Implements abstract javax.mail.Message.getRecipients.

Returns the recepients specified by the type.
public Address[]
getReplyTo()

Overrides javax.mail.Message.getReplyTo.

Return the value of the RFC 822 "Reply-To" header field.
public Address

Returns:

Address object
getSender
()

Returns the value of the RFC 822 "Sender" header field.

public Date

Returns:

The sent Date
getSentDate
()

Implements abstract javax.mail.Message.getSentDate.

Returns the value of the RFC 822 "Date" field.
public int

Returns:

size of content in bytes
getSize
()

Implements javax.mail.Part.getSize.

Return the size of the content of this message in bytes.
public String

Returns:

Subject
getSubject
()

Implements abstract javax.mail.Message.getSubject.

Returns the value of the "Subject" header field.
private void
initStrict()

Set the strict flag based on property.

public boolean

Returns:

true if it matches the MIME type
isMimeType
(String
the MIME type to check
mimeType
)

Implements javax.mail.Part.isMimeType.

Is this Part of the specified MIME type?
public synchronized boolean

Returns:

value of the specified flag for this message
isSet
(Flags.Flag
the flag
flag
)

Overrides javax.mail.Message.isSet.

Check whether the flag specified in the flag argument is set in this message.
protected void
parse(InputStream
The message input stream
is
)

Parse the InputStream setting the headers and content fields appropriately.

public void
removeHeader(String
the name of this header
name
)

Implements javax.mail.Part.removeHeader.

Remove all headers with this name.
public Message

Returns:

the reply Message
reply
(boolean
reply should be sent to all recipients of this message
replyToAll
)

Implements abstract javax.mail.Message.reply.

Get a new Message suitable for a reply to this message.
public Message

Returns:

the reply Message
reply
(boolean
reply should be sent to all recipients of this message
replyToAll
,
boolean
set the ANSWERED flag in this message?
setAnswered
)

Get a new Message suitable for a reply to this message.

public void
saveChanges()

Implements abstract javax.mail.Message.saveChanges.

Updates the appropriate header fields of this message to be consistent with the message's contents.
private void
setAddressHeader(String name, Address[] addresses)

public void
setContent(Object
the content object
o
,
String
Mime type of the object
type
)

Implements javax.mail.Part.setContent.

A convenience method for setting this Message's content.
public void
setContent(Multipart
The multipart object that is the Message's content
mp
)

Implements javax.mail.Part.setContent.

This method sets the Message's content to a Multipart object.
public void
setContentID(String
the content ID
cid
)

Set the "Content-ID" header field of this Message.

public void
setContentLanguage(String[]
array of language tags
languages
)

Implements javax.mail.internet.MimePart.setContentLanguage.

Set the "Content-Language" header of this MimePart.
public void
setContentMD5(String
the MD5 value
md5
)

Implements javax.mail.internet.MimePart.setContentMD5.

Set the "Content-MD5" header field of this Message.
public synchronized void
setDataHandler(DataHandler
The DataHandler for the content.
dh
)

Implements javax.mail.Part.setDataHandler.

This method provides the mechanism to set this part's content.
public void
setDescription(String
content-description
description
)

Implements javax.mail.Part.setDescription.

Set the "Content-Description" header field for this Message.
public void
setDescription(String
Description
description
,
String
Charset for encoding
charset
)

Set the "Content-Description" header field for this Message.

public void
setDisposition(String
disposition of this part
disposition
)

Implements javax.mail.Part.setDisposition.

Set the disposition in the "Content-Disposition" header field of this body part.
public void
setFileName(String
Filename to associate with this part
filename
)

Implements javax.mail.Part.setFileName.

Set the filename associated with this part, if possible.
public synchronized void
setFlags(Flags
Flags object containing the flags to be set
flag
,
boolean
the value to be set
set
)

Implements abstract javax.mail.Message.setFlags.

Set the flags for this message.
public void
setFrom(Address
the sender of this message
address
)

Implements abstract javax.mail.Message.setFrom.

Set the RFC 822 "From" header field.
public void
setFrom(String
the sender(s) of this message
address
)

Set the RFC 822 "From" header field.

public void
setFrom()

Implements abstract javax.mail.Message.setFrom.

Set the RFC 822 "From" header field using the value of the InternetAddress.getLocalAddress method.
public void
setHeader(String
header name
name
,
String
header value
value
)

Implements javax.mail.Part.setHeader.

Set the value for this header_name.
public void
setRecipients(Message.RecipientType
Recipient type
type
,
Address[]
Addresses
addresses
)

Implements abstract javax.mail.Message.setRecipients.

Set the specified recipient type to the given addresses.
public void
setRecipients(Message.RecipientType
Recipient type
type
,
String
Addresses
addresses
)

Set the specified recipient type to the given addresses.

public void
setReplyTo(Address[]
addresses to which replies should be directed
addresses
)

Overrides javax.mail.Message.setReplyTo.

Set the RFC 822 "Reply-To" header field.
public void
setSender(Address
the sender of this message
address
)

Set the RFC 822 "Sender" header field.

public void
setSentDate(Date
the sent date of this message
d
)

Implements abstract javax.mail.Message.setSentDate.

Set the RFC 822 "Date" header field.
public void
setSubject(String
The subject
subject
)

Implements abstract javax.mail.Message.setSubject.

Set the "Subject" header field.
public void
setSubject(String
The subject
subject
,
String
The charset
charset
)

Set the "Subject" header field.

public void
setText(String
the text content to set
text
)

Implements javax.mail.internet.MimePart.setText, javax.mail.Part.setText.

Convenience method that sets the given String as this part's content, with a MIME type of "text/plain".
public void
setText(String
the text content to set
text
,
String
the charset to use for the text
charset
)

Implements javax.mail.internet.MimePart.setText.

Convenience method that sets the given String as this part's content, with a MIME type of "text/plain" and the specified charset.
public void
setText(String
the text content to set
text
,
String
the charset to use for the text
charset
,
String
the MIME subtype to use (e.g., "html")
subtype
)

Implements javax.mail.internet.MimePart.setText.

Convenience method that sets the given String as this part's content, with a primary MIME type of "text" and the specified MIME subtype.
protected synchronized void
updateHeaders()

Called by the saveChanges method to actually update the MIME headers.

protected void
updateMessageID()

Update the Message-ID header.

public void
writeTo(OutputStream
the stream to write to
os
)

Implements javax.mail.Part.writeTo.

Output the message as an RFC 822 format stream.
public void
writeTo(OutputStream
the stream to write to
os
,
String[]
the headers to not include in the output
ignoreList
)

Output the message as an RFC 822 format stream, without specified headers.

Inherited from javax.mail.Message:
addRecipientgetFoldergetMessageNumbergetSessionisExpungedmatchsetExpungedsetFlagsetMessageNumbersetRecipient