导入jar包
邮箱参数配置
1
2
3
4
5
6
7#Spring Boot Starter Mail
smtp.qq.com =
465 =
邮箱 =
授权码 =
smtps =
true =使用JavaMailSender发送邮件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21private static final Logger logger = LoggerFactory.getLogger(MailClient.class);
private JavaMailSender mailSender;
"${spring.mail.username}") (
private String from;
public void sendMail(String to, String subject, String content) {
try {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
mailSender.send(helper.getMimeMessage());
} catch (MessagingException e) {
logger.error("发送邮件失败:" + e.getMessage());
}
}
使用Thymeleaf发送HTML文件
1 |
|