码迷,mamicode.com
首页 > 编程语言 > 详细

spring boot集成email

时间:2020-05-21 09:34:48      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:mem   monit   log   boot   mpi   mes   aging   star   port   

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

标签:mem   monit   log   boot   mpi   mes   aging   star   port   

原文地址:https://www.cnblogs.com/sam-cheng/p/12928245.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!