POM依赖
<!-- javaMailSender 邮寄发送配置 -->
<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>
- 主配置文件application.properties
#javaMailSender 邮件发送的配置
#smtp.163.com
spring.mail.host=smtp.qq.com
spring.mail.username=9420175@qq.com
#授权码
spring.mail.password=omkgfxkfyzzpbcda
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
获取发件人
@Component
public class MailSendConfig {
@Value("${spring.mail.username}") // 获取配置文件中的值
private String emailFrom;
public String getEmailFrom() {
return emailFrom;
}
public void setEmailFrom(String emailFrom) {
this.emailFrom = emailFrom;
}
}
业务层写法:
@Autowired
private MailSendConfig mailSendConfig;
@Autowired
private JavaMailSender javaMail;
@Override
public void sendMail(String user, String title, String context) {
// 简单邮件发送
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(mailSendConfig.getEmailFrom());
message.setTo("1284038873@qq.com");
message.setSubject("1284038873@qq.com");
message.setText("你是谁啊");
javaMail.send(message);
}
- 发送带附件的邮件
// 发送带附件的邮件
@Override
public void sendMail(String user, String title, String context, File file) {
MimeMessage message = javaMail.createMimeMessage();
try {
// 添加附件,第二个参数必须设置为true
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(mailSendConfig.getEmailFrom());
helper.setTo("1284038873@qq.com");
helper.setSubject(title);// 主题
helper.setText(context);// 内容
FileSystemResource resource = new FileSystemResource(file);
helper.addAttachment("附件", resource);
} catch (MessagingException e) {
e.printStackTrace();
}
javaMail.send(message);// 发送邮件
}
控制层
File file = new File("src/main/resources/static/Spring.pdf");
emailService.sendMail("1284038873@qq.com", "你好", "带附件", file);
- 发送模板邮件
增加freemarker 模板依赖
<!-- freemarker 模板引擎 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
业务层写法
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
// 发送模板模板邮件
@Override
public void sendTemplate(String user, String title, String info) {
MimeMessage message = javaMail.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(mailSendConfig.getEmailFrom());
helper.setTo("1284038873@qq.com");
helper.setSubject(title);// 主题
// 封装模板使用的数据
Map<String, Object> map = new HashMap<>();
map.put("username", "http://www.baidu.com");
// 得到模板
Template template = freeMarkerConfigurer.getConfiguration().getTemplate(info);
String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
helper.setText(html, true);
} catch (Exception e) {
e.printStackTrace();
}
javaMail.send(message);
}
控制层写法
emailService.sendTemplate(null, "模板", "info.html");