1.添加pom对邮件的支持
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2.配置邮件参数
spring.mail.host=smtp.qq.com
spring.mail.username=227746885@qq.com
spring.mail.password=。。。。 //授权码
spring.mail.default-encoding=utf-8
3.发送简单文本邮件
@Value("${spring.mail.username}")
private String from; //发送人
@Autowired
private JavaMailSender mailSender;
public void sendSimpleMail(String to,String subject,String content) {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setTo(to);//接收人
simpleMailMessage.setSubject(subject); //邮件主题
simpleMailMessage.setText(content);//邮件内容
simpleMailMessage.setFrom(from);//发送人
mailSender.send(simpleMailMessage);
}
4.发送带html的邮件
public void sendHtmlMain(String to,String subject,String content) throws Exception {
MimeMessage mssage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mssage,true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true); //true为使用html
helper.setFrom(from);
mailSender.send(mssage);
}
@Test
public void testHtmlMail() throws Exception {
String to="813457063@qq.com";
String subject="html邮件";
String content="<html>\n"+
"<body>\n"+
"<h3>hello 你好 html 邮件</h3>\n"+
"</body>\n"+
"</html>";
mailService.sendHtmlMain(to, subject, content);
}
5.发送带有附件的邮件
public void sendAttachmentsMail(String to,String subject,String content,String filePath) throws Exception {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true);
helper.setFrom(from);
//filepath 为附件的路径
FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath));
String fileName=fileSystemResource.getFilename();
helper.addAttachment(fileName, fileSystemResource);
mailSender.send(message);
}
//带附件的
@Test
public void testAttachmentsMail() throws Exception {
String to="813457063@qq.com";
String subject="附件的邮件";
String content="<html>\n"+
"<body>\n"+
"<h3>hello 你好 附件 邮件</h3>\n"+
"</body>\n"+
"</html>";
String filePath="F:\\99.GIF";
mailService.sendAttachmentsMail(to, subject, content,filePath);
}
5.发送html里面有图片的邮件
public void sendInLinResourceMail(String to,String subject,String content,String rscPath,String rscId) throws Exception {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true);
helper.setFrom(from);
FileSystemResource fileSystemResource = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, fileSystemResource);
mailSender.send(message);
}
//图片在html中的
@Test
public void testInLinResourceMail() throws Exception {
String to="813457063@qq.com";
String subject="图片在html中的邮件";
String rscId="wq001";
String filePath="F:\\99.GIF";
String content="<html>\n"+
"<body>\n"+
"<h3>hello 图片在html中的 邮件</h3>\n"+
"<img src='cid:"+rscId+"></img>"+
"</body>\n"+
"</html>";
mailService.sendInLinResourceMail(to, subject, content,filePath,rscId);
}
6. 和thymeleaf集成模板邮件
添加thymeleaf支持
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
templates文件夹下面新建 emailTemplate.html文件
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>邮件模板</title>
</head>
<body>
这是一份注册邮箱感谢支持,,。。。。。。。测试。。。。 点击下面的链接
<a href="#" th:href="@{http://www.baidu.com/{id}(id=${id})}">激活账号 </a>
</body>
</html>
test方法
@Resource
TemplateEngine templateEngine;
//模板邮件
@Test
public void testTmeplateMail() throws Exception {
Context context = new Context();
context.setVariable("id", "008");
String emailContent=templateEngine.process("emailTemplate", context);
String to="813457063@qq.com";
String subject="模板中的邮件";
mailService.sendHtmlMain(to, subject, emailContent);;
}
关于异常捕获:
private final Logger logger=(Logger) LoggerFactory.getLogger(this.getClass());
public void sendInLinResourceMail(String to,String subject,String content,String rscPath,String rscId) {
logger.info("开始发送邮件。。。。{},{},{},{},{}",to,subject,content,rscPath,rscId);
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper;
try {
helper = new MimeMessageHelper(message,true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true);
helper.setFrom(from);
FileSystemResource fileSystemResource = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, fileSystemResource);
mailSender.send(message);
logger.info("发送邮件成功。。。。。。。。。");
} catch (MessagingException e) {
logger.error("发送邮件失败。。。。。。。。。");
}
}
邮件发送到此结束
以后可以考虑 做微服务,或者放队列