Multipart消息在java中以text / plain而不是multipart / alternative的形式发送

时间:2018-02-07 15:41:24

标签: java javamail mime multipart

我发送的信息是多部分,下面是我的代码:

Message message = new MimeMessage(session);
Multipart multiPart = new MimeMultipart("alternative");

try {

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

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

multiPart.addBodyPart(htmlPart);
multiPart.addBodyPart(textPart);
message.setContent(multiPart);
transport.send(message);

} catch (AddressException e) {
    e.printStackTrace();
    System.out.println("Error: "+e.getMessage());

}

现在,当我在Outlook中收到它时,它显示为 text / plain

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我终于找到了解决这个问题的方法。显然,有时您需要保存更改,以便将您的消息作为multipart / alternative发送。我提到了:

https://docs.oracle.com/javaee/6/api/javax/mail/Message.html#saveChanges()

现在我的代码是:

Message message = new MimeMessage(session);
Multipart multiPart = new MimeMultipart("alternative");

try {

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

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

multiPart.addBodyPart(htmlPart);
multiPart.addBodyPart(textPart);
message.setContent(multiPart);
message.saveChanges();
transport.send(message);

} catch (AddressException e) {
    e.printStackTrace();
    System.out.println("Error: "+e.getMessage());

}