How to use html tags in a multipart email message. When I use <b>
its not recognized as the bold tag.
Multipart is a way of encoding (possibly) multiple data elements inside a single body. It may mean that there are attachments or other items along with the text body but that is not necessary. You can also just encode a single message body in a multipart message.
What Is Multipart/Alternative? Multipart/alternative emails contain both plain text and HTML. What a user sees depends on their set preferences or what's allowed by their email client. If an email client can't render HTML messages, it will display the plain text version.
A multipart type is one which represents a document that's comprised of multiple component parts, each of which may have its own individual MIME type; or, a multipart type may encapsulate multiple files being sent together in one transaction.
The Multipart/Related media type is intended for compound objects consisting of several inter-related body parts. For a Multipart/Related object, proper display cannot be achieved by individually displaying the constituent body parts. The content-type of the Multipart/Related object is specified by the type parameter.
Ah, you're using Java.
Note that in my opinion, you should always set a plain text alternative in a HTML email.
This code also lets you inline images (referenced from the HTML with <img src="cid:foo">
, but not all email clients support this.
MimeMessage mm = prepareMessage(from, to, subject, cc, bcc);
MimeMultipart mp = new MimeMultipart("alternative");
// Attach Plain Text
MimeBodyPart plain = new MimeBodyPart();
plain.setText(plainText);
mp.addBodyPart(plain);
/*
* Any attached images for the HTML portion of the email need to be encapsulated with
* the HTML portion within a 'related' MimeMultipart. Hence we create one of these and
* set it as a bodypart for the overall message.
*/
MimeMultipart htmlmp = new MimeMultipart("related");
MimeBodyPart htmlbp = new MimeBodyPart();
htmlbp.setContent(htmlmp);
mp.addBodyPart(htmlbp);
// Attach HTML Text
MimeBodyPart html = new MimeBodyPart();
html.setContent(htmlText, "text/html");
htmlmp.addBodyPart(html);
// Attach template images (EmailImage is a simple class that holds image data)
for (EmailImage ei : template.getImages()) {
MimeBodyPart img = new MimeBodyPart();
img.setContentID(ei.getFilename());
img.setFileName(ei.getFilename());
ByteArrayDataSource bads = new ByteArrayDataSource(ei.getImageData(), ei.getMimeType());
img.setDataHandler(new DataHandler(bads));
htmlmp.addBodyPart(img);
}
mm.setContent(mp);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With