【发布时间】:2018-09-24 11:49:13
【问题描述】:
我正在发送包含 text/plain 和 text/html 的多部分电子邮件,但是当我在 Outlook 中收到邮件时,html 内容作为附件出现,而 text/plain 出现在正文中。我想要两个都在体内。
pom.xml 配置是这样的
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.2</version>
</dependency>
而java代码是
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.put("mail.smtp.host", sSMTPServer);
props.put("mail.smtp.port", 25);
Session session = null;
session = Session.getInstance(props, null);
MimeMessage msg = new MimeMessage(session);
Multipart mainMultipart = new MimeMultipart("mixed");
Multipart htmlAndTextMultipart = new MimeMultipart("alternative");
MimeBodyPart BodyPart = new MimeBodyPart();
BodyPart.setText(Header);
htmlAndTextMultipart.addBodyPart(BodyPart);
MimeBodyPart BodyPart1 = new MimeBodyPart();
BodyPart1.setContent(Body, "text/html; charset=utf-8");
htmlAndTextMultipart.addBodyPart(BodyPart1);
for (int i = 0; i < htmlAndTextMultipart.getCount(); i++) {
mainMultipart.addBodyPart(htmlAndTextMultipart.getBodyPart(i));
}
msg.setContent(mainMultipart);
InternetAddress[] from = InternetAddress.parse("appdev@abc.com");
InternetAddress[] toList = InternetAddress.parse(to);
msg.addFrom(from);
msg.addRecipients(Message.RecipientType.TO, toList);
msg.setSubject("Multipart_Testing");
Transport transport = session.getTransport("smtp");
transport.connect(sSMTPServer, 25, null,
null);
transport.sendMessage(msg, toList);
System.out.println("Sent");
transport.close();
}
问题仅在于 HTML 内容没有出现在正文中的 Outlook
在 Outlook 中,所有内容都不会像 gmail 那样呈现,而是作为附件呈现
mail snippet of outlook
【问题讨论】:
标签: java jakarta-mail