<!-- 邮件end -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!--freemarker模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
# JavaMailSender 邮件发送的配置s
spring.mail.host=smtp.qq.com
spring.mail.username=97248@qq.com
spring.mail.password=klfncwbbhd
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
# JavaMailSender 邮件发送的配置e
@Autowired
private JavaMailSender mailSender; //自动注入的Bean
@Value("${spring.mail.username}")
private String mailFrom; //读取配置文件中的参数
@Value("${spring.mail.password}")
private String password; //读取配置文件中的参数
private String mailTo = "1336956709@qq.com"; // 加载收件人地址
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer; //自动注入
@RequestMapping(value = "/sendMail", method = RequestMethod.GET)
public void sendMail() {
// 测试
MimeMessage message = null;
try {
message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(mailFrom);
helper.setTo(mailTo);
helper.setSubject("主题:额度发放");
Map<String, Object> model = new HashMap<>();
model.put("resTypeName", "服务资源");
model.put("resName", "小车00001");
model.put("quota", "10000");
//读取 html 模板
Template template = freeMarkerConfigurer.getConfiguration().getTemplate("mailtemplats/sendQuota.ftl");
String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
helper.setText(html, true);
System.out.println("发送成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.toString());
System.out.println("发送失败");
}
mailSender.send(message);
// 测试
}