如何在多部分电子邮件消息中使用HTML标签。当我使用 <b>
标签时,它不被识别为加粗标签。
<img src="cid:foo">
),但不是所有的电子邮件客户端都支持这一点。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);
回答这个问题有点困难。事情不能正常工作的原因有很多。以下是一些您可以检查以尝试隔离问题的事项。
其他HTML标签是否被识别?例如<p>
,<a>
?如果是,请尝试使用<strong>
标签代替<b>
标签。
在电子邮件阅读器中检查消息源。也许在发送之前,'<'或'>'字符已经被转义为'<'或'>'。
您是否尝试在不同的阅读器中查看电子邮件,例如Webmail或桌面应用程序?
尝试使用CSS更改字体粗细:
.important-text { font-weight: bold; }
<span class=".important-text">Super important text</span>
你是否在电子邮件消息的那部分设置了Content-type为text/html?
或者你是在使用Outlook查看吗?Outlook的查看器使用Word来呈现HTML,而不是像任何明智的设计一样使用IE渲染引擎。这意味着会丢失重要的格式。
另外,尝试使用不同的字体。有些字体没有定义粗体变体。虽然这是一个小概率事件,但大多数字体渲染技术可以自动将非粗体字体加粗。