spring boot集成email
Posted samcheng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot集成email相关的知识,希望对你有一定的参考价值。
1.在application.properties中增加下列属性
#monitor mail config
spring.mail.host=smtp.mxhichina.com(根据邮箱来定,此示例是阿里云企业邮箱)
spring.mail.port=25(根据邮箱来定)
spring.mail.username=邮箱
spring.mail.password=
2.gradle依赖
compile(‘org.springframework.boot:spring-boot-starter-mail:2.1.3.RELEASE‘)
3.创建实现
@Autowired
JavaMailSender mailSender;
/**
*send mail in the form of html
*/
public void sendHtmlMail(String subject, String text) {
MimeMessage message = mailSender.createMimeMessage();
String subjectContent = config.getMailContentTemplate();
try {
//true means that needs to create a multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(config.getMailFrom());
helper.setTo(config.getMailTo().split(","));
helper.setSubject(subjectContent.replace("{monitor type}", subject));
helper.setText(text, true);
mailSender.send(message);
LOG.info("send monitor mail successfully");
} catch (MessagingException e) {
LOG.error("send monitor mail error", e);
}
}
以上是关于spring boot集成email的主要内容,如果未能解决你的问题,请参考以下文章
Spring boot:thymeleaf 没有正确渲染片段