Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multipart email message

How to use html tags in a multipart email message. When I use <b> its not recognized as the bold tag.

like image 931
Harish Avatar asked Nov 12 '09 10:11

Harish


People also ask

What is a multipart email python?

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?

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.

What is multipart MIME type?

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.

What is multipart related content-type?

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.


1 Answers

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);
like image 154
JeeBee Avatar answered Sep 19 '22 05:09

JeeBee