springboot邮件发送学习
一、依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
二、配置application.properties
spring.mail.host=smtp.qq.com spring.mail.port=587 spring.mail.username=1525702966@qq.com spring.mail.password=ubknfzhjkhrbbabe spring.mail.default-encoding=UTF-8 spring.mail.properties.mail.smtp.socketFactoryClass=javax.net.ssl.SSLSocketFactory spring.mail.properties.mail.debug=true
配置含义分别如下:
配置 SMTP 服务器地址
SMTP 服务器的端口
配置邮箱用户名
配置密码,注意,不是真正的密码,而是刚刚申请到的授权码
默认的邮件编码
配饰 SSL 加密工厂
表示开启 DEBUG 模式,这样,邮件发送过程的日志会在控制台打印出来,方便排查错误
如果不知道 smtp 服务器的端口或者地址的的话,可以参考 腾讯的邮箱文档
https://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=371
做完这些之后,Spring Boot 就会自动帮我们配置好邮件发送类,相关的配置在org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration 类中,部分源码如下:
@Configuration @ConditionalOnClass({ MimeMessage.class, MimeType.class, MailSender.class }) @ConditionalOnMissingBean(MailSender.class) @Conditional(MailSenderCondition.class) @EnableConfigurationProperties(MailProperties.class) @Import({ MailSenderJndiConfiguration.class, MailSenderPropertiesConfiguration.class }) public class MailSenderAutoConfiguration { }
从这段代码中,可以看到,导入了另外一个配置 MailSenderPropertiesConfiguration 类,这个类中,提供了邮件发送相关的工具类:
@Configuration @ConditionalOnProperty(prefix = "spring.mail", name = "host") class MailSenderPropertiesConfiguration { private final MailProperties properties; MailSenderPropertiesConfiguration(MailProperties properties) { this.properties = properties; } @Bean @ConditionalOnMissingBean public JavaMailSenderImpl mailSender() { JavaMailSenderImpl sender = new JavaMailSenderImpl(); applyProperties(sender); return sender; } }
可以看到,这里创建了一个 JavaMailSenderImpl 的实例, JavaMailSenderImpl 是 JavaMailSender 的一个实现,我们将使用 JavaMailSenderImpl 来完成邮件的发送工作。
做完如上两步,邮件发送的准备工作就算是完成了,接下来就可以直接发送邮件了。
具体的发送,有 5 种不同的方式,我们一个一个来看。
发送简单邮件
简单邮件就是指邮件内容是一个普通的文本文档:
@Autowired JavaMailSender javaMailSender; @Test public void sendSimpleMail() { SimpleMailMessage message = new SimpleMailMessage(); message.setSubject("这是一封测试邮件"); message.setFrom("1525702966@qq.com"); message.setTo("25xxxxx755@qq.com"); //message.setCc("37xxxxx37@qq.com"); //message.setBcc("14xxxxx098@qq.com"); message.setSentDate(new Date()); message.setText("这是测试邮件的正文"); javaMailSender.send(message); }
从上往下,代码含义分别如下:
构建一个邮件对象
设置邮件主题
设置邮件发送者
设置邮件接收者,可以有多个接收者
设置邮件抄送人,可以有多个抄送人
设置隐秘抄送人,可以有多个
设置邮件发送日期
设置邮件的正文
发送邮件
最后执行该方法,就可以实现邮件的发送
邮件的内容都是比较的丰富的,所以大部分邮件都是通过 HTML 来呈现的,如果直接拼接 HTML 字符串,这样以后不好维护,为了解决这个问题,一般邮件发送,都会有相应的邮件模板。最具代表性的两个模板就是 Freemarker 模板和 Thyemeleaf 模板了。
三、使用Thymeleaf作为邮件模板
推荐在 Spring Boot 中使用 Thymeleaf 来构建邮件模板。因为 Thymeleaf 的自动化配置提供了一个 TemplateEngine,通过 TemplateEngine 可以方便的将 Thymeleaf 模板渲染为 HTML ,同时,Thymeleaf 的自动化配置在这里是继续有效的 。
导入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
创建Thymeleaf模板
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <p>hello 欢迎加入 xxx 大家庭,您的入职信息如下:</p> <table border="1"> <tr> <td>姓名</td> <td th:text="${username}"></td> </tr> <tr> <td>工号</td> <td th:text="${num}"></td> </tr> <tr> <td>薪水</td> <td th:text="${salary}"></td> </tr> </table> <div style="color: #ff1a0e">一起努力创造辉煌</div> </body> </html>
接下来发送邮件:
@Autowired TemplateEngine templateEngine; @Test public void sendThymeleafMail() throws MessagingException { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setSubject("这是一封测试邮件"); helper.setFrom("1510161612@qq.com"); helper.setTo("25xxxxx755@qq.com"); helper.setCc("37xxxxx37@qq.com"); helper.setBcc("14xxxxx098@qq.com"); helper.setSentDate(new Date()); Context context = new Context(); context.setVariable("username", "boy"); context.setVariable("num","000001"); context.setVariable("salary", "99999"); String process = templateEngine.process("mail.html", context); helper.setText(process,true); javaMailSender.send(mimeMessage); }
- SpringBoot学习-(二十三)SpringBoot发送邮件
- Spring Boot整合邮件发送与注意事项
- Spring Boot发送邮件详解
- springBoot发送邮件
- 47. Spring Boot发送邮件【从零开始学Spring Boot】
- 基于spring-boot的邮件发送的代码
- SpringBoot 发送邮件功能实现
- Spring Boot中利用JavaMailSender发送邮件的方法示例(附源码)
- springboot发送邮件
- Spring Boot 邮件发送
- Spring Boot 发送邮件功能案例分析
- Spring boot下实现邮件发送功能
- spring boot 发送邮件
- springboot中发送各种类型的邮件
- springboot 邮件发送
- SpringBoot发送QQ邮件
- SpringBoot(十一):SpringBoot发送邮件
- springboot发送邮件
- Spring boot发送邮件失败时候的解决办法
- SpringBoot2.x系列第十四章 (发送邮件)