Skip to Main Content
Java Enterprise Best Practices
book

Java Enterprise Best Practices

by O'Reilly Java Authors
December 2002
Intermediate to advanced content levelIntermediate to advanced
288 pages
9h 46m
English
O'Reilly Media, Inc.
Content preview from Java Enterprise Best Practices

Use Related Alternatives

The related MIME type alone does not provide a plain-text alternative for browsers that don’t support pretty graphics. If you add a text part to the preceding message, the text will display along with the HTML, rather than instead of it. Fortunately, JavaMail allows you to nest Multipart objects, so you can create a top-level “multipart/alternative” and include both a plain-text option and a “multipart/related” section containing HTML and supporting resources.

Multipart objects cannot be added directly to each other, but must be wrapped into a MimeBodyPart first. You can easily extend the earlier example to do this:

Multipart topLevel = new MimeMultipart("alternative");
   
MimeBodyPart htmlContent = new MimeBodyPart(  );
htmlContent.setContent(multipartRelated);
MimeBodyPart textAlternate = new MimeBodyPart(  );
textAlternate.setText("Guidelines");
     
topLevel.addBodyPart(textAlternate);    
topLevel.addBodyPart(htmlContent);
msg.setContent(topLevel);

Recipients with HTML capability will now see the HTML version with the embedded graphics, and users with text-only capability will see the text version. Older clients will see everything, but placing the text part at the beginning of the message ensures that they’ll be able to make some sense of what’s going on.

Of course, the XSL approach can also be used to create the plain text and HTML content for multipart/alternative and multipart/related messages.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Moving to Java 9: Better Design and Simpler Code

Moving to Java 9: Better Design and Simpler Code

Trisha Gee
Java EE 8 High Performance

Java EE 8 High Performance

Romain Manni-Bucau

Publisher Resources

ISBN: 0596003846Supplemental ContentErrata Page