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 Multipart/Related for Rich Messages

To ensure that they can be read as easily as possible, richly formatted messages should be completely self-contained, instead of relying on images or resources available on a server. The MIME standard provides another special multipart type: “related”. Related parts can refer to each other via URIs pointing to the Content-ID, or “CID,” associated with each part. A CID URI takes the form “cid:contentid”. You can use the setHeader( ) method of MimeBodyPart to create known content IDs for each message part. Once this is done, HTML parts can, for example, display an image by referring to the message part containing the image data.

The following example contains an HTML part that includes an <img> tag pointing to the "cid:myimage" URI. It also contains a second part containing a GIF image with a content ID of "myimage".

 . . . Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress("images@company.com")); msg.setRecipient(Message.RecipientType.TO, new InternetAddress("bob.cratchett@company.com")); msg.setSubject("Image Email"); Multipart multipartRelated = new MimeMultipart("related"); MimeBodyPart mbp = new MimeBodyPart( ); mbp.setContent( "<html><body><img src=\"cid:myimage\"></body></html>", "text/html"); multipartRelated.addBodyPart(mbp); . . . MimeBodyPart mbp2 = new MimeBodyPart( ); mbp2.setDataHandler(new DataHandler(new FileDataSource("c:\\inline.gif"))); mbp2.setFileName("inline.gif"); mbp2.setHeader("Content-ID","myimage"); ...
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