为什么 Multipart/mixed java 邮件内容在 outlook 中作为附件发送?

Why Multipart/mixed java mail content is sent as attachment in outlook?

我正在发送包含 text/plain 和 text/html 的多部分电子邮件,但是当我在 outlook 中收到邮件时 html 内容作为附件出现并且 text/plain body 即将到来。 body.

我都想要

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 内容不会出现在 body

mail snippet of gmail

并且在 Outlook 中,所有内容都不会像 Gmail 那样呈现,而是作为附件出现
mail snippet of outlook

您无法控制邮件程序如何显示您的邮件,不同的邮件程序会以不同方式显示相同的邮件。最好的办法是将所有内容放入一个 html 部分,并坚持使用非常基本的 html.

哦,JavaMail 1.4.2 非常 旧,如果可能,您应该upgrade to the current version