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);
}
}