标签:pid 服务 smt void ica xmlns 用户名 boot artifact
<!-- 发送邮件依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
PS: 在这里因为使用的是QQ邮箱,而QQ邮箱的软件收发服务是默认关闭的,需要手动打开
进入邮箱->账户设置
spring:
mail:
host: smtp.qq.com
username: 邮箱用户名
password: 密码或验证码
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
@Autowired
private JavaMailSender mailSender;
@Test
public void sendSimpleMail() throws Exception {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("邮件发送方");
message.setTo("邮件接收方");
message.setSubject("主题");
message.setText("正文");
mailSender.send(message);
}
<!-- 引入thymeleaf模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
# thymeleaf 参数配置
spring:
thymeleaf:
cache: true
prefix: classpath:/templates/
suffix: .html
encoding: UTF-8
servlet:
content-type: text/html
check-template-location: true
enabled: true
mode: HTML5
<html xmlns:th="http://www.w3.org/1999/xhtml">
<body>
<p>
吾儿,<span th:text="${username}">错误</span>,最后一次测试了!!!
</p>
</body>
</html>
@Autowired
private TemplateEngine templateEngine;
@Autowired
private JavaMailSender mailSender;
public void sendTemplateMail() throws Exception{
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setFrom("发送方");
helper.setTo("接收方");
helper.setSubject("主题");
Context context = new Context();
context.setVariable("username","name");
String emailContent = templateEngine.process("template",context);
helper.setText(emailContent,true);
mailSender.send(message);
}
标签:pid 服务 smt void ica xmlns 用户名 boot artifact
原文地址:https://www.cnblogs.com/JRookie/p/8868280.html