Best Practices - Sending javamail mime multipart emails - and gmail

前端 未结 1 938
清歌不尽
清歌不尽 2020-12-01 10:42

I have a Tomcat application that needs to send confirmation emails etc. I have coded the emailer with Javamail (mail.jar) to send multipart text/html emails. I based the cod

相关标签:
1条回答
  • 2020-12-01 11:18

    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...

    i.e.

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

    Thanks for the help!

    0 讨论(0)
提交回复
热议问题