0

我正在使用以下代码,它在浏览器中运行良好,但在 Outlook 中显示的格式不正确:

MimeMultipart mimeMultipart = new MimeMultipart();
MimeBodyPart bodyPart = new MimeBodyPart();

bodyPart.setText("<html><body><font size='3px' face='Times New Roman'>"
    + removeMultipleSpaces(msgbody) + "</font></body></html>");
mimeMultipart.addBodyPart(bodyPart);
bodyPart.setDescription("Text");
bodyPart.setHeader("Content-Type", "text/html; charset=utf-8" );
4

1 回答 1

0

正如您在评论中提到的,您想要添加图像。为图像创建一个 MimeBodyPart,创建一个 cid,并在图像的 MimeBodyPart 上执行 setContentID:

import javax.mail.internet.*;

MimeMultipart content = new MimeMultipart("related");

MimeBodyPart mainPart = new MimeBodyPart();
mainPart.setText("<html><body><img src=\"cid:123456789@localhost\"></body></html>","UTF-8", "html");
content.addBodyPart(mainPart);

MimeBodyPart imagePart = new MimeBodyPart();

java.net.URL img1 = Example.class.getClassLoader().getResource("image.png");
imagePart.setDataHandler(new DataHandler(img1));
//or:
//imagePart.attachFile("resources/image.png");

imagePart.setContentID("<123456789@localhost>");   // to embed
imagePart.setDisposition(MimeBodyPart.INLINE);  // to embed
content.addBodyPart(imagePart);
于 2017-05-10T08:29:29.997 回答