Multipart Messages
Up until now, we haven’t discussed viewing or creating
messages except at a very simplistic level. Let’s close this chapter
by taking a look at the MimeMessage
class, the standard Message
implementation for Internet email, and the Multipart
class, which allows MIME-formatted
messages that contain multiple parts (for instance, text and HTML
versions of content and file attachments).
Displaying Multipart Messages
MimeMessage
objects are
either single part or multipart. A single-part message has a
content-type attribute matching the content of the message (often
“text/plain”); the actual message content is stored in the message’s
content attribute, accessed via the getContent()
method.
Multipart messages of content types begin with “multipart” and
contain a Multipart
object as
their content. The Multipart
object contains a series of BodyPart
objects (in the case of a
MIME-formatted message, these will be MimeBodyPart
objects and can be cast as
such). Each BodyPart
has its own
content type and associated content.
The getContent()
and
getContentType()
methods of
Message
are actually inherited
from the Part
interface, which is
also implemented by BodyPart
and
MimeBodyPart
. Part
provides another useful method,
isMimeType()
, which allows
testing for particular content types. The isMimeType()
method will compare only the
primary and subcontent types (the types immediately before and after
the / character). So, isMimeType("text/plain")
will return true if the MIME type is ...
Get Java Enterprise in a Nutshell, Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.