解决Spring Boot Mail无法发送HTML格式的邮件
Spring Boot版本:2.6.2
查阅spring-boot-starter-mail源码的MimeMessageHelper.setText方法,发现有个Boolean类型参数控制是否是HTML格式,方法签名如下:
public void setText(String text, boolean html) throws MessagingException
MimeMessage mailMessage = mailSender.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, Boolean.TRUE, StandardCharsets.UTF_8.toString()); messageHelper.setSubject(subject);
// Boolean.TRUE设置HTML格式文本 messageHelper.setText(loadTemplate(message.getMessage()), Boolean.TRUE); messageHelper.setFrom(from); messageHelper.setTo(message.getTo()); mailSender.send(mailMessage);