本文展示一下如何使用spring mail来发送html邮件。
<!-- email -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
public void send(String from, String[] toMails, String subject, String text,
Map<String,Object> inlines) throws Exception{
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(from);
helper.setTo(toMails);
helper.setSubject(subject);
helper.setText(text, true); //支持html
// 增加inline
if(inlines != null){
for(Map.Entry<String,Object> entry: inlines.entrySet()){
if(entry.getValue() instanceof ClassPathResource){
helper.addInline(entry.getKey(), (Resource) entry.getValue());
}
}
}
mailSender.send(mimeMessage);
}