如何使用 JavaMail 发送 html 电子邮件?

Posted

技术标签:

【中文标题】如何使用 JavaMail 发送 html 电子邮件?【英文标题】:How do I send an html email with JavaMail? 【发布时间】:2012-10-29 05:54:58 【问题描述】:

我已经在this link尝试了字符集建议

但是电子邮件显示的 messageText 的确切值...没有呈现任何 html

这是我当前的代码

import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

String messageText = "<br/>THIS IS A TEST...<br/>!!!";

Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.auth", "true"); 
props.setProperty("mail.smtp.ssl.enable", "true");

Session mailSession = Session.getInstance(props, null);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();

MimeMessage message = new MimeMessage(mailSession);
message.setSubject(messageSubject);
message.setContent(messageText, "text/html; charset=utf-8");

Address[] fromAddress = InternetAddress.parse ( "pleasedonotreplymessage@[removed]" ) ; 
message.addFrom( fromAddress );

message.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));

transport.connect("[removed]", "", "");
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();

我宁愿必须安装其他方工具..这将需要对我当前的代码进行彻底的返工。

【问题讨论】:

如果可用,最好使用经过测试且可信的框架。除非你想自己学习如何编写这样的框架:-) 但是 JavaMail API 是 Java 本身的核心......并且被广泛使用。也不需要额外的安装、配置和/或设置不同的工具。对吗? 【参考方案1】:

这是经过测试并确认可以工作的。

HTML 电子邮件有更优雅的结构可用于更广泛的电子邮件客户端支持,但对于快速解决方案,这适用于我测试过的阅读器(Outlook、连接到 Exchange 的 android 邮件客户端,以及邮箱)。

public static void sendHtmlEmail(String server, String from, String to, String cc, String subject, String htmlBody) throws MessagingException 
    Properties props = new Properties();
    props.setProperty("mail.smtp.host", server);
    Session session = Session.getInstance(props);

    MimeMessage msg = new MimeMessage(session);
    msg.setFrom(from);
    msg.setRecipients(RecipientType.TO, to);
    msg.setRecipients(RecipientType.CC, cc);
    msg.setSubject(subject);
    msg.setSentDate(new Date());

    MimeMultipart mp = new MimeMultipart();
    MimeBodyPart part = new MimeBodyPart();
    part.setText(htmlBody);
    mp.addBodyPart(part);
    msg.setContent(mp);

    // Content type has to be set after the message is put together
    // Then saveChanges() must be called for it to take effect
    part.setHeader("Content-Type", "text/html");
    msg.saveChanges();

    Transport.send(msg);

【讨论】:

【参考方案2】:

您可以使用 Thymeleaf 呈现丰富的 HTML 电子邮件并使用 Spring Mail 实用程序发送。

教程:http://www.thymeleaf.org/doc/articles/springmail.html

教程源码:https://github.com/thymeleaf/thymeleafexamples-springmail

【讨论】:

【参考方案3】:

使用Javamail API

Here is an Example

【讨论】:

我不是已经在用了吗?您提供的链接使用“Message msg = new MimeMessage(mailSession);”这正是我所拥有的。它还使用“Transport transport = mailSession.getTransport();”而我正是这样......?

以上是关于如何使用 JavaMail 发送 html 电子邮件?的主要内容,如果未能解决你的问题,请参考以下文章

基于JavaMail的Java邮件发送:简单邮件发送

无法使用 javamail 将 HTML 电子邮件发送到 Gmail

Spring通过Gmail SMTP服务器MailSender发送电子邮件

springboot集成junit测试与javamail测试遇到的问题

使用 javamail api 发送的 html 电子邮件正文在 Outlook 中出现乱码

JavaMail API 发送电子邮件