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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. * @author yangkai.shen
  94. * @date Created in 2018-11-21 11:16
  95. */
  96. public interface MailService {
  97. /**
  98. * 发送文本邮件
  99. *
  100. * @param to 收件人地址
  101. * @param subject 邮件主题
  102. * @param content 邮件内容
  103. * @param cc 抄送地址
  104. */
  105. void sendSimpleMail(String to, String subject, String content, String... cc);
  106. /**
  107. * 发送HTML邮件
  108. *
  109. * @param to 收件人地址
  110. * @param subject 邮件主题
  111. * @param content 邮件内容
  112. * @param cc 抄送地址
  113. * @throws MessagingException 邮件发送异常
  114. */
  115. void sendHtmlMail(String to, String subject, String content, String... cc) throws MessagingException;
  116. /**
  117. * 发送带附件的邮件
  118. *
  119. * @param to 收件人地址
  120. * @param subject 邮件主题
  121. * @param content 邮件内容
  122. * @param filePath 附件地址
  123. * @param cc 抄送地址
  124. * @throws MessagingException 邮件发送异常
  125. */
  126. void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc) throws MessagingException;
  127. /**
  128. * 发送正文中有静态资源的邮件
  129. *
  130. * @param to 收件人地址
  131. * @param subject 邮件主题
  132. * @param content 邮件内容
  133. * @param rscPath 静态资源地址
  134. * @param rscId 静态资源id
  135. * @param cc 抄送地址
  136. * @throws MessagingException 邮件发送异常
  137. */
  138. void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc) throws MessagingException;
  139. }
  140. ```
  141. ## MailServiceImpl.java
  142. ```java
  143. /**
  144. * <p>
  145. * 邮件接口
  146. * </p>
  147. *
  148. * @author yangkai.shen
  149. * @date Created in 2018-11-21 13:49
  150. */
  151. @Service
  152. public class MailServiceImpl implements MailService {
  153. @Autowired
  154. private JavaMailSender mailSender;
  155. @Value("${spring.mail.username}")
  156. private String from;
  157. /**
  158. * 发送文本邮件
  159. *
  160. * @param to 收件人地址
  161. * @param subject 邮件主题
  162. * @param content 邮件内容
  163. * @param cc 抄送地址
  164. */
  165. @Override
  166. public void sendSimpleMail(String to, String subject, String content, String... cc) {
  167. SimpleMailMessage message = new SimpleMailMessage();
  168. message.setFrom(from);
  169. message.setTo(to);
  170. message.setSubject(subject);
  171. message.setText(content);
  172. if (ArrayUtil.isNotEmpty(cc)) {
  173. message.setCc(cc);
  174. }
  175. mailSender.send(message);
  176. }
  177. /**
  178. * 发送HTML邮件
  179. *
  180. * @param to 收件人地址
  181. * @param subject 邮件主题
  182. * @param content 邮件内容
  183. * @param cc 抄送地址
  184. * @throws MessagingException 邮件发送异常
  185. */
  186. @Override
  187. public void sendHtmlMail(String to, String subject, String content, String... cc) throws MessagingException {
  188. MimeMessage message = mailSender.createMimeMessage();
  189. MimeMessageHelper helper = new MimeMessageHelper(message, true);
  190. helper.setFrom(from);
  191. helper.setTo(to);
  192. helper.setSubject(subject);
  193. helper.setText(content, true);
  194. if (ArrayUtil.isNotEmpty(cc)) {
  195. helper.setCc(cc);
  196. }
  197. mailSender.send(message);
  198. }
  199. /**
  200. * 发送带附件的邮件
  201. *
  202. * @param to 收件人地址
  203. * @param subject 邮件主题
  204. * @param content 邮件内容
  205. * @param filePath 附件地址
  206. * @param cc 抄送地址
  207. * @throws MessagingException 邮件发送异常
  208. */
  209. @Override
  210. public void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc) throws MessagingException {
  211. MimeMessage message = mailSender.createMimeMessage();
  212. MimeMessageHelper helper = new MimeMessageHelper(message, true);
  213. helper.setFrom(from);
  214. helper.setTo(to);
  215. helper.setSubject(subject);
  216. helper.setText(content, true);
  217. if (ArrayUtil.isNotEmpty(cc)) {
  218. helper.setCc(cc);
  219. }
  220. FileSystemResource file = new FileSystemResource(new File(filePath));
  221. String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
  222. helper.addAttachment(fileName, file);
  223. mailSender.send(message);
  224. }
  225. /**
  226. * 发送正文中有静态资源的邮件
  227. *
  228. * @param to 收件人地址
  229. * @param subject 邮件主题
  230. * @param content 邮件内容
  231. * @param rscPath 静态资源地址
  232. * @param rscId 静态资源id
  233. * @param cc 抄送地址
  234. * @throws MessagingException 邮件发送异常
  235. */
  236. @Override
  237. public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc) throws MessagingException {
  238. MimeMessage message = mailSender.createMimeMessage();
  239. MimeMessageHelper helper = new MimeMessageHelper(message, true);
  240. helper.setFrom(from);
  241. helper.setTo(to);
  242. helper.setSubject(subject);
  243. helper.setText(content, true);
  244. if (ArrayUtil.isNotEmpty(cc)) {
  245. helper.setCc(cc);
  246. }
  247. FileSystemResource res = new FileSystemResource(new File(rscPath));
  248. helper.addInline(rscId, res);
  249. mailSender.send(message);
  250. }
  251. }
  252. ```
  253. ## MailServiceTest.java
  254. ```java
  255. /**
  256. * <p>
  257. * 邮件测试
  258. * </p>
  259. *
  260. * @author yangkai.shen
  261. * @date Created in 2018-11-21 13:49
  262. */
  263. public class MailServiceTest extends SpringBootDemoEmailApplicationTests {
  264. @Autowired
  265. private MailService mailService;
  266. @Autowired
  267. private TemplateEngine templateEngine;
  268. @Autowired
  269. private ApplicationContext context;
  270. /**
  271. * 测试简单邮件
  272. */
  273. @Test
  274. public void sendSimpleMail() {
  275. mailService.sendSimpleMail("237497819@qq.com", "这是一封简单邮件", "这是一封普通的SpringBoot测试邮件");
  276. }
  277. /**
  278. * 测试HTML邮件
  279. *
  280. * @throws MessagingException 邮件异常
  281. */
  282. @Test
  283. public void sendHtmlMail() throws MessagingException {
  284. Context context = new Context();
  285. context.setVariable("project", "Spring Boot Demo");
  286. context.setVariable("author", "Yangkai.Shen");
  287. context.setVariable("url", "https://github.com/xkcoding/spring-boot-demo");
  288. String emailTemplate = templateEngine.process("welcome", context);
  289. mailService.sendHtmlMail("237497819@qq.com", "这是一封模板HTML邮件", emailTemplate);
  290. }
  291. /**
  292. * 测试HTML邮件,自定义模板目录
  293. *
  294. * @throws MessagingException 邮件异常
  295. */
  296. @Test
  297. public void sendHtmlMail2() throws MessagingException {
  298. SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
  299. templateResolver.setApplicationContext(context);
  300. templateResolver.setCacheable(false);
  301. templateResolver.setPrefix("classpath:/email/");
  302. templateResolver.setSuffix(".html");
  303. templateEngine.setTemplateResolver(templateResolver);
  304. Context context = new Context();
  305. context.setVariable("project", "Spring Boot Demo");
  306. context.setVariable("author", "Yangkai.Shen");
  307. context.setVariable("url", "https://github.com/xkcoding/spring-boot-demo");
  308. String emailTemplate = templateEngine.process("test", context);
  309. mailService.sendHtmlMail("237497819@qq.com", "这是一封模板HTML邮件", emailTemplate);
  310. }
  311. /**
  312. * 测试附件邮件
  313. *
  314. * @throws MessagingException 邮件异常
  315. */
  316. @Test
  317. public void sendAttachmentsMail() throws MessagingException {
  318. URL resource = ResourceUtil.getResource("static/xkcoding.png");
  319. mailService.sendAttachmentsMail("237497819@qq.com", "这是一封带附件的邮件", "邮件中有附件,请注意查收!", resource.getPath());
  320. }
  321. /**
  322. * 测试静态资源邮件
  323. *
  324. * @throws MessagingException 邮件异常
  325. */
  326. @Test
  327. public void sendResourceMail() throws MessagingException {
  328. String rscId = "xkcoding";
  329. String content = "<html><body>这是带静态资源的邮件<br/><img src=\'cid:" + rscId + "\' ></body></html>";
  330. URL resource = ResourceUtil.getResource("static/xkcoding.png");
  331. mailService.sendResourceMail("237497819@qq.com", "这是一封带静态资源的邮件", content, resource.getPath(), rscId);
  332. }
  333. }
  334. ```
  335. ## welcome.html
  336. > 此文件为邮件模板,位于 resources/templates 目录下
  337. ```html
  338. <!DOCTYPE html>
  339. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  340. <head>
  341. <meta charset="UTF-8">
  342. <title>SpringBootDemo(入门SpringBoot的首选Demo)</title>
  343. <style>
  344. body {
  345. text-align: center;
  346. margin-left: auto;
  347. margin-right: auto;
  348. }
  349. #welcome {
  350. text-align: center;
  351. }
  352. </style>
  353. </head>
  354. <body>
  355. <div id="welcome">
  356. <h3>欢迎使用 <span th:text="${project}"></span> - Powered By <span th:text=" ${author}"></span></h3>
  357. <span th:text="${url}"></span>
  358. <div style="text-align: center; padding: 10px">
  359. <a style="text-decoration: none;" href="#" th:href="@{${url}}" target="_bank">
  360. <strong>spring-boot-demo,入门Spring Boot的首选Demo!:)</strong>
  361. </a>
  362. </div>
  363. <div style="text-align: center; padding: 4px">
  364. 如果对你有帮助,请任意打赏
  365. </div>
  366. <div style="width: 100%;height: 100%;text-align: center;display: flex">
  367. <div style="flex: 1;"></div>
  368. <div style="display: flex;width: 400px;">
  369. <div style="flex: 1;text-align: center;">
  370. <div>
  371. <img width="180px" height="180px" src="http://xkcoding.com/resources/wechat-reward-image.png">
  372. </div>
  373. <div>微信打赏</div>
  374. </div>
  375. <div style="flex: 1;text-align: center;">
  376. <div><img width="180px" height="180px" src="http://xkcoding.com/resources/alipay-reward-image.png">
  377. </div>
  378. <div>支付宝打赏</div>
  379. </div>
  380. </div>
  381. <div style="flex: 1;"></div>
  382. </div>
  383. </div>
  384. </body>
  385. </html>
  386. ```
  387. ## 参考
  388. - Spring Boot 官方文档:https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-email
  389. - Spring Boot 官方文档:https://docs.spring.io/spring/docs/5.1.2.RELEASE/spring-framework-reference/integration.html#mail

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