gpt4 book ai didi

java - 多部分电子邮件

转载 作者:太空狗 更新时间:2023-10-29 15:27:51 26 4
gpt4 key购买 nike

如何在多部分电子邮件中使用 html 标签。当我使用 <b>它不被识别为粗体标记。

最佳答案

啊,你正在使用 Java。

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

此代码还允许您内联图像(从带有 <img src="cid:foo"> 的 HTML 引用,但并非所有电子邮件客户端都支持此功能。

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

关于java - 多部分电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1721220/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com