多部分电子邮件 [英] Multipart email message

查看:193
本文介绍了多部分电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在多部分电子邮件中使用html标签。

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

推荐答案

请注意,在我看来,您应该始终在HTML电子邮件中设置纯文本替代方法。

Note that in my opinion, you should always set a plain text alternative in a HTML email.

此代码还可让您内联图片(从HTML中引用< img src =cid:foo> ,但并不是所有的电子邮件客户端都支持这一点。

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);

这篇关于多部分电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆