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.