JavaMail-发送纯HTML内容而不格式化

时间:2018-11-16 08:27:27

标签: html javamail mime-types content-type

我想使用JavaMail发送未格式化HTML标记的电子邮件。

我使用以下代码:

public void sendMail() throws MessagingException, IllegalStateException, IllegalArgumentException {

    Properties properties = System.getProperties();
    properties.setProperty("mail.smtp.host", "localhost");

    Session session = Session.getDefaultInstance(properties);

    MimeMessage message = new MimeMessage(session);

    MimeMultipart multipart = new MimeMultipart();

    MimeBodyPart messageBodyPart = new MimeBodyPart();

    messageBodyPart.setContent("<h1>This is an HTML message</h1>", "text/plain; charset=UTF-8");

    multipart.addBodyPart(messageBodyPart);

    message.setContent(multipart);

    message.setFrom(new InternetAddress("noreply@localhost.com"));

    message.addRecipient(Message.RecipientType.TO, new InternetAddress("to@localhost.com"));

    message.setSubject("HTML mail");

    Transport.send(message);
}

我收到HTML格式的电子邮件:

  

这是HTML消息

但是我想要html纯文本:

  

这是HTML消息

感谢帮助!

1 个答案:

答案 0 :(得分:0)

使用message.setText()代替message.setContent()