O'Reilly logo

Java Enterprise in a Nutshell, Third Edition by William Crawford, Jim Farley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required