You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 14 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. # spring-boot-demo-email
  2. > 此 demo 主要演示了 Spring Boot 如何整合邮件功能,包括发送简单文本邮件、HTML邮件(包括模板HTML邮件)、附件邮件、静态资源邮件。
  3. ## pom.xml
  4. ```xml
  5. <?xml version="1.0" encoding="UTF-8"?>
  6. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  8. <modelVersion>4.0.0</modelVersion>
  9. <artifactId>spring-boot-demo-email</artifactId>
  10. <version>1.0.0-SNAPSHOT</version>
  11. <packaging>jar</packaging>
  12. <name>spring-boot-demo-email</name>
  13. <description>Demo project for Spring Boot</description>
  14. <parent>
  15. <groupId>com.xkcoding</groupId>
  16. <artifactId>spring-boot-demo</artifactId>
  17. <version>1.0.0-SNAPSHOT</version>
  18. </parent>
  19. <properties>
  20. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  21. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  22. <java.version>1.8</java.version>
  23. <jasypt.version>2.1.1</jasypt.version>
  24. </properties>
  25. <dependencies>
  26. <!-- Spring Boot 邮件依赖 -->
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-mail</artifactId>
  30. </dependency>
  31. <!--jasypt配置文件加解密-->
  32. <dependency>
  33. <groupId>com.github.ulisesbocchio</groupId>
  34. <artifactId>jasypt-spring-boot-starter</artifactId>
  35. <version>${jasypt.version}</version>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-starter-test</artifactId>
  40. <scope>test</scope>
  41. </dependency>
  42. <dependency>
  43. <groupId>cn.hutool</groupId>
  44. <artifactId>hutool-all</artifactId>
  45. </dependency>
  46. <!-- Spring Boot 模板依赖 -->
  47. <dependency>
  48. <groupId>org.springframework.boot</groupId>
  49. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  50. </dependency>
  51. </dependencies>
  52. <build>
  53. <finalName>spring-boot-demo-email</finalName>
  54. <plugins>
  55. <plugin>
  56. <groupId>org.springframework.boot</groupId>
  57. <artifactId>spring-boot-maven-plugin</artifactId>
  58. </plugin>
  59. </plugins>
  60. </build>
  61. </project>
  62. ```
  63. ## application.yml
  64. ```yaml
  65. spring:
  66. mail:
  67. host: smtp.mxhichina.com
  68. port: 465
  69. username: spring-boot-demo@xkcoding.com
  70. # 使用 jasypt 加密密码,使用com.xkcoding.email.PasswordTest.testGeneratePassword 生成加密密码,替换 ENC(加密密码)
  71. password: ENC(OT0qGOpXrr1Iog1W+fjOiIDCJdBjHyhy)
  72. protocol: smtp
  73. test-connection: true
  74. default-encoding: UTF-8
  75. properties:
  76. mail.smtp.auth: true
  77. mail.smtp.starttls.enable: true
  78. mail.smtp.starttls.required: true
  79. mail.smtp.ssl.enable: true
  80. mail.display.sendmail: spring-boot-demo
  81. # 为 jasypt 配置解密秘钥
  82. jasypt:
  83. encryptor:
  84. password: spring-boot-demo
  85. ```
  86. ## MailService.java
  87. ```java
  88. /**
  89. * <p>
  90. * 邮件接口
  91. * </p>
  92. *
  93. * @package: com.xkcoding.email.service
  94. * @description: 邮件接口
  95. * @author: yangkai.shen
  96. * @date: Created in 2018/11/21 11:16
  97. * @copyright: Copyright (c) 2018
  98. * @version: V1.0
  99. * @modified: yangkai.shen
  100. */
  101. public interface MailService {
  102. /**
  103. * 发送文本邮件
  104. *
  105. * @param to 收件人地址
  106. * @param subject 邮件主题
  107. * @param content 邮件内容
  108. * @param cc 抄送地址
  109. */
  110. void sendSimpleMail(String to, String subject, String content, String... cc);
  111. /**
  112. * 发送HTML邮件
  113. *
  114. * @param to 收件人地址
  115. * @param subject 邮件主题
  116. * @param content 邮件内容
  117. * @param cc 抄送地址
  118. * @throws MessagingException 邮件发送异常
  119. */
  120. void sendHtmlMail(String to, String subject, String content, String... cc) throws MessagingException;
  121. /**
  122. * 发送带附件的邮件
  123. *
  124. * @param to 收件人地址
  125. * @param subject 邮件主题
  126. * @param content 邮件内容
  127. * @param filePath 附件地址
  128. * @param cc 抄送地址
  129. * @throws MessagingException 邮件发送异常
  130. */
  131. void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc) throws MessagingException;
  132. /**
  133. * 发送正文中有静态资源的邮件
  134. *
  135. * @param to 收件人地址
  136. * @param subject 邮件主题
  137. * @param content 邮件内容
  138. * @param rscPath 静态资源地址
  139. * @param rscId 静态资源id
  140. * @param cc 抄送地址
  141. * @throws MessagingException 邮件发送异常
  142. */
  143. void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc) throws MessagingException;
  144. }
  145. ```
  146. ## MailServiceImpl.java
  147. ```java
  148. /**
  149. * <p>
  150. * 邮件接口
  151. * </p>
  152. *
  153. * @package: com.xkcoding.email.service.impl
  154. * @description: 邮件接口
  155. * @author: yangkai.shen
  156. * @date: Created in 2018/11/21 13:49
  157. * @copyright: Copyright (c) 2018
  158. * @version: V1.0
  159. * @modified: yangkai.shen
  160. */
  161. @Service
  162. public class MailServiceImpl implements MailService {
  163. @Autowired
  164. private JavaMailSender mailSender;
  165. @Value("${spring.mail.username}")
  166. private String from;
  167. /**
  168. * 发送文本邮件
  169. *
  170. * @param to 收件人地址
  171. * @param subject 邮件主题
  172. * @param content 邮件内容
  173. * @param cc 抄送地址
  174. */
  175. @Override
  176. public void sendSimpleMail(String to, String subject, String content, String... cc) {
  177. SimpleMailMessage message = new SimpleMailMessage();
  178. message.setFrom(from);
  179. message.setTo(to);
  180. message.setSubject(subject);
  181. message.setText(content);
  182. if (ArrayUtil.isNotEmpty(cc)) {
  183. message.setCc(cc);
  184. }
  185. mailSender.send(message);
  186. }
  187. /**
  188. * 发送HTML邮件
  189. *
  190. * @param to 收件人地址
  191. * @param subject 邮件主题
  192. * @param content 邮件内容
  193. * @param cc 抄送地址
  194. * @throws MessagingException 邮件发送异常
  195. */
  196. @Override
  197. public void sendHtmlMail(String to, String subject, String content, String... cc) throws MessagingException {
  198. MimeMessage message = mailSender.createMimeMessage();
  199. MimeMessageHelper helper = new MimeMessageHelper(message, true);
  200. helper.setFrom(from);
  201. helper.setTo(to);
  202. helper.setSubject(subject);
  203. helper.setText(content, true);
  204. if (ArrayUtil.isNotEmpty(cc)) {
  205. helper.setCc(cc);
  206. }
  207. mailSender.send(message);
  208. }
  209. /**
  210. * 发送带附件的邮件
  211. *
  212. * @param to 收件人地址
  213. * @param subject 邮件主题
  214. * @param content 邮件内容
  215. * @param filePath 附件地址
  216. * @param cc 抄送地址
  217. * @throws MessagingException 邮件发送异常
  218. */
  219. @Override
  220. public void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc) throws MessagingException {
  221. MimeMessage message = mailSender.createMimeMessage();
  222. MimeMessageHelper helper = new MimeMessageHelper(message, true);
  223. helper.setFrom(from);
  224. helper.setTo(to);
  225. helper.setSubject(subject);
  226. helper.setText(content, true);
  227. if (ArrayUtil.isNotEmpty(cc)) {
  228. helper.setCc(cc);
  229. }
  230. FileSystemResource file = new FileSystemResource(new File(filePath));
  231. String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
  232. helper.addAttachment(fileName, file);
  233. mailSender.send(message);
  234. }
  235. /**
  236. * 发送正文中有静态资源的邮件
  237. *
  238. * @param to 收件人地址
  239. * @param subject 邮件主题
  240. * @param content 邮件内容
  241. * @param rscPath 静态资源地址
  242. * @param rscId 静态资源id
  243. * @param cc 抄送地址
  244. * @throws MessagingException 邮件发送异常
  245. */
  246. @Override
  247. public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc) throws MessagingException {
  248. MimeMessage message = mailSender.createMimeMessage();
  249. MimeMessageHelper helper = new MimeMessageHelper(message, true);
  250. helper.setFrom(from);
  251. helper.setTo(to);
  252. helper.setSubject(subject);
  253. helper.setText(content, true);
  254. if (ArrayUtil.isNotEmpty(cc)) {
  255. helper.setCc(cc);
  256. }
  257. FileSystemResource res = new FileSystemResource(new File(rscPath));
  258. helper.addInline(rscId, res);
  259. mailSender.send(message);
  260. }
  261. }
  262. ```
  263. ## MailServiceTest.java
  264. ```java
  265. /**
  266. * <p>
  267. * 邮件测试
  268. * </p>
  269. *
  270. * @package: com.xkcoding.email.service
  271. * @description: 邮件测试
  272. * @author: yangkai.shen
  273. * @date: Created in 2018/11/21 13:49
  274. * @copyright: Copyright (c) 2018
  275. * @version: V1.0
  276. * @modified: yangkai.shen
  277. */
  278. public class MailServiceTest extends SpringBootDemoEmailApplicationTests {
  279. @Autowired
  280. private MailService mailService;
  281. @Autowired
  282. private TemplateEngine templateEngine;
  283. @Autowired
  284. private ApplicationContext context;
  285. /**
  286. * 测试简单邮件
  287. */
  288. @Test
  289. public void sendSimpleMail() {
  290. mailService.sendSimpleMail("237497819@qq.com", "这是一封简单邮件", "这是一封普通的SpringBoot测试邮件");
  291. }
  292. /**
  293. * 测试HTML邮件
  294. *
  295. * @throws MessagingException 邮件异常
  296. */
  297. @Test
  298. public void sendHtmlMail() throws MessagingException {
  299. Context context = new Context();
  300. context.setVariable("project", "Spring Boot Demo");
  301. context.setVariable("author", "Yangkai.Shen");
  302. context.setVariable("url", "https://github.com/xkcoding/spring-boot-demo");
  303. String emailTemplate = templateEngine.process("welcome", context);
  304. mailService.sendHtmlMail("237497819@qq.com", "这是一封模板HTML邮件", emailTemplate);
  305. }
  306. /**
  307. * 测试HTML邮件,自定义模板目录
  308. *
  309. * @throws MessagingException 邮件异常
  310. */
  311. @Test
  312. public void sendHtmlMail2() throws MessagingException {
  313. SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
  314. templateResolver.setApplicationContext(context);
  315. templateResolver.setCacheable(false);
  316. templateResolver.setPrefix("classpath:/email/");
  317. templateResolver.setSuffix(".html");
  318. templateEngine.setTemplateResolver(templateResolver);
  319. Context context = new Context();
  320. context.setVariable("project", "Spring Boot Demo");
  321. context.setVariable("author", "Yangkai.Shen");
  322. context.setVariable("url", "https://github.com/xkcoding/spring-boot-demo");
  323. String emailTemplate = templateEngine.process("test", context);
  324. mailService.sendHtmlMail("237497819@qq.com", "这是一封模板HTML邮件", emailTemplate);
  325. }
  326. /**
  327. * 测试附件邮件
  328. *
  329. * @throws MessagingException 邮件异常
  330. */
  331. @Test
  332. public void sendAttachmentsMail() throws MessagingException {
  333. URL resource = ResourceUtil.getResource("static/xkcoding.png");
  334. mailService.sendAttachmentsMail("237497819@qq.com", "这是一封带附件的邮件", "邮件中有附件,请注意查收!", resource.getPath());
  335. }
  336. /**
  337. * 测试静态资源邮件
  338. *
  339. * @throws MessagingException 邮件异常
  340. */
  341. @Test
  342. public void sendResourceMail() throws MessagingException {
  343. String rscId = "xkcoding";
  344. String content = "<html><body>这是带静态资源的邮件<br/><img src=\'cid:" + rscId + "\' ></body></html>";
  345. URL resource = ResourceUtil.getResource("static/xkcoding.png");
  346. mailService.sendResourceMail("237497819@qq.com", "这是一封带静态资源的邮件", content, resource.getPath(), rscId);
  347. }
  348. }
  349. ```
  350. ## welcome.html
  351. > 此文件为邮件模板,位于 resources/templates 目录下
  352. ```html
  353. <!DOCTYPE html>
  354. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  355. <head>
  356. <meta charset="UTF-8">
  357. <title>SpringBootDemo(入门SpringBoot的首选Demo)</title>
  358. <style>
  359. body {
  360. text-align: center;
  361. margin-left: auto;
  362. margin-right: auto;
  363. }
  364. #welcome {
  365. text-align: center;
  366. }
  367. </style>
  368. </head>
  369. <body>
  370. <div id="welcome">
  371. <h3>欢迎使用 <span th:text="${project}"></span> - Powered By <span th:text=" ${author}"></span></h3>
  372. <span th:text="${url}"></span>
  373. <div style="text-align: center; padding: 10px">
  374. <a style="text-decoration: none;" href="#" th:href="@{${url}}" target="_bank">
  375. <strong>spring-boot-demo,入门Spring Boot的首选Demo!:)</strong>
  376. </a>
  377. </div>
  378. <div style="text-align: center; padding: 4px">
  379. 如果对你有帮助,请任意打赏
  380. </div>
  381. <div style="width: 100%;height: 100%;text-align: center;display: flex">
  382. <div style="flex: 1;"></div>
  383. <div style="display: flex;width: 400px;">
  384. <div style="flex: 1;text-align: center;">
  385. <div>
  386. <img width="180px" height="180px" src="http://xkcoding.com/resources/wechat-reward-image.png">
  387. </div>
  388. <div>微信打赏</div>
  389. </div>
  390. <div style="flex: 1;text-align: center;">
  391. <div><img width="180px" height="180px" src="http://xkcoding.com/resources/alipay-reward-image.png">
  392. </div>
  393. <div>支付宝打赏</div>
  394. </div>
  395. </div>
  396. <div style="flex: 1;"></div>
  397. </div>
  398. </div>
  399. </body>
  400. </html>
  401. ```
  402. ## 参考
  403. - Spring Boot 官方文档:https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-email
  404. - Spring Boot 官方文档:https://docs.spring.io/spring/docs/5.1.2.RELEASE/spring-framework-reference/integration.html#mail

一个用来深度学习并实战 spring boot 的项目,目前总共包含 66 个集成demo,已经完成 55 个。