且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

***实践 - 发送javamail mime多部分电子邮件 - 和gmail

更新时间:2022-02-08 21:30:40

解决了!似乎根据多部分MIME规范,部件的顺序很重要。它们应按从低保真度到高保真度的顺序添加。因此,似乎GMail遵循规范并使用最后一部分。在我的情况下,我有他们HTML,文本。我刚刚将订单交换为文字,HTML和Gmail正确呈现...

Solved! It seems according to the multipart MIME spec, the order of the parts are important. They should be added in order from low fidelity to high fidelity. So it seems GMail follows the spec and uses the last part. In my case I had them HTML, Text. I just swapped the order to Text, HTML and Gmail renders it correctly...

ie

MimeBodyPart textPart = new MimeBodyPart();
textPart.setText(text, "utf-8");

MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(html, "text/html; charset=utf-8");

multiPart.addBodyPart(textPart); // <-- first
multiPart.addBodyPart(htmlPart); // <-- second
message.setContent(multiPart);

感谢您的帮助!