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 13 kB

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

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

Contributors (1)