diff --git a/spring-boot-demo-email/README.md b/spring-boot-demo-email/README.md new file mode 100644 index 0000000..90e39bc --- /dev/null +++ b/spring-boot-demo-email/README.md @@ -0,0 +1,417 @@ +# spring-boot-demo-email + +> 此 demo 主要演示了 Spring Boot 如何整合邮件功能,包括发送简单文本邮件、HTML邮件(包括模板HTML邮件)、附件邮件、静态资源邮件。 + +## pom.xml + +```xml + + + 4.0.0 + + spring-boot-demo-email + 1.0.0-SNAPSHOT + jar + + spring-boot-demo-email + Demo project for Spring Boot + + + com.xkcoding + spring-boot-demo + 1.0.0-SNAPSHOT + + + + UTF-8 + UTF-8 + 1.8 + + + + + + org.springframework.boot + spring-boot-starter-mail + + + + org.springframework.boot + spring-boot-starter-test + test + + + + cn.hutool + hutool-all + + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + + spring-boot-demo-email + + + org.springframework.boot + spring-boot-maven-plugin + + + + + +``` + +## application.yml + +> 此处特别感谢 **路神**([@路小磊](https://github.com/looly)) 的开源工具集 [Hutool](https://github.com/looly/hutool) ,此处邮箱使用 Hutool 提供的公共邮箱做为测试邮箱,请童鞋们不要违规使用。 + +```yaml +spring: + mail: + host: smtp.yeah.net + port: 994 + username: hutool@yeah.net + password: q1w2e3 + protocol: smtp + test-connection: true + default-encoding: UTF-8 + properties: + mail.smtp.auth: true + mail.smtp.starttls.enable: true + mail.smtp.starttls.required: true + mail.smtp.ssl.enable: true + mail.display.sendmail: xkcoding +``` + +## MailService.java + +```java +/** + *

+ * 邮件接口 + *

+ * + * @package: com.xkcoding.email.service + * @description: 邮件接口 + * @author: yangkai.shen + * @date: Created in 2018/11/21 11:16 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +public interface MailService { + /** + * 发送文本邮件 + * + * @param to 收件人地址 + * @param subject 邮件主题 + * @param content 邮件内容 + * @param cc 抄送地址 + */ + void sendSimpleMail(String to, String subject, String content, String... cc); + + /** + * 发送HTML邮件 + * + * @param to 收件人地址 + * @param subject 邮件主题 + * @param content 邮件内容 + * @param cc 抄送地址 + * @throws MessagingException 邮件发送异常 + */ + void sendHtmlMail(String to, String subject, String content, String... cc) throws MessagingException; + + /** + * 发送带附件的邮件 + * + * @param to 收件人地址 + * @param subject 邮件主题 + * @param content 邮件内容 + * @param filePath 附件地址 + * @param cc 抄送地址 + * @throws MessagingException 邮件发送异常 + */ + void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc) throws MessagingException; + + /** + * 发送正文中有静态资源的邮件 + * + * @param to 收件人地址 + * @param subject 邮件主题 + * @param content 邮件内容 + * @param rscPath 静态资源地址 + * @param rscId 静态资源id + * @param cc 抄送地址 + * @throws MessagingException 邮件发送异常 + */ + void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc) throws MessagingException; + +} +``` + +## MailServiceImpl.java + +```java +/** + *

+ * 邮件接口 + *

+ * + * @package: com.xkcoding.email.service.impl + * @description: 邮件接口 + * @author: yangkai.shen + * @date: Created in 2018/11/21 13:49 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Service +public class MailServiceImpl implements MailService { + @Autowired + private JavaMailSender mailSender; + @Value("${spring.mail.username}") + private String from; + + /** + * 发送文本邮件 + * + * @param to 收件人地址 + * @param subject 邮件主题 + * @param content 邮件内容 + * @param cc 抄送地址 + */ + @Override + public void sendSimpleMail(String to, String subject, String content, String... cc) { + SimpleMailMessage message = new SimpleMailMessage(); + message.setFrom(from); + message.setTo(to); + message.setSubject(subject); + message.setText(content); + if (ArrayUtil.isNotEmpty(cc)) { + message.setCc(cc); + } + mailSender.send(message); + } + + /** + * 发送HTML邮件 + * + * @param to 收件人地址 + * @param subject 邮件主题 + * @param content 邮件内容 + * @param cc 抄送地址 + * @throws MessagingException 邮件发送异常 + */ + @Override + public void sendHtmlMail(String to, String subject, String content, String... cc) throws MessagingException { + MimeMessage message = mailSender.createMimeMessage(); + MimeMessageHelper helper = new MimeMessageHelper(message, true); + helper.setFrom(from); + helper.setTo(to); + helper.setSubject(subject); + helper.setText(content, true); + if (ArrayUtil.isNotEmpty(cc)) { + helper.setCc(cc); + } + mailSender.send(message); + } + + /** + * 发送带附件的邮件 + * + * @param to 收件人地址 + * @param subject 邮件主题 + * @param content 邮件内容 + * @param filePath 附件地址 + * @param cc 抄送地址 + * @throws MessagingException 邮件发送异常 + */ + @Override + public void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc) throws MessagingException { + MimeMessage message = mailSender.createMimeMessage(); + + MimeMessageHelper helper = new MimeMessageHelper(message, true); + helper.setFrom(from); + helper.setTo(to); + helper.setSubject(subject); + helper.setText(content, true); + if (ArrayUtil.isNotEmpty(cc)) { + helper.setCc(cc); + } + FileSystemResource file = new FileSystemResource(new File(filePath)); + String fileName = filePath.substring(filePath.lastIndexOf(File.separator)); + helper.addAttachment(fileName, file); + + mailSender.send(message); + } + + /** + * 发送正文中有静态资源的邮件 + * + * @param to 收件人地址 + * @param subject 邮件主题 + * @param content 邮件内容 + * @param rscPath 静态资源地址 + * @param rscId 静态资源id + * @param cc 抄送地址 + * @throws MessagingException 邮件发送异常 + */ + @Override + public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc) throws MessagingException { + MimeMessage message = mailSender.createMimeMessage(); + + MimeMessageHelper helper = new MimeMessageHelper(message, true); + helper.setFrom(from); + helper.setTo(to); + helper.setSubject(subject); + helper.setText(content, true); + if (ArrayUtil.isNotEmpty(cc)) { + helper.setCc(cc); + } + FileSystemResource res = new FileSystemResource(new File(rscPath)); + helper.addInline(rscId, res); + + mailSender.send(message); + } +} +``` + +## MailServiceTest.java + +```java +/** + *

+ * 邮件测试 + *

+ * + * @package: com.xkcoding.email.service + * @description: 邮件测试 + * @author: yangkai.shen + * @date: Created in 2018/11/21 13:49 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +public class MailServiceTest extends SpringBootDemoEmailApplicationTests { + @Autowired + private MailService mailService; + @Autowired + private TemplateEngine templateEngine; + + /** + * 测试简单邮件 + */ + @Test + public void sendSimpleMail() { + mailService.sendSimpleMail("237497819@qq.com", "这是一封简单邮件", "这是一封普通的SpringBoot测试邮件"); + } + + /** + * 测试HTML邮件 + * + * @throws MessagingException 邮件异常 + */ + @Test + public void sendHtmlMail() throws MessagingException { + Context context = new Context(); + context.setVariable("project", "Spring Boot Demo"); + context.setVariable("author", "yangkai.shen"); + context.setVariable("url", "https://github.com/xkcoding/spring-boot-demo"); + + String emailTemplate = templateEngine.process("welcome", context); + mailService.sendHtmlMail("237497819@qq.com", "这是一封模板HTML邮件", emailTemplate); + } + + /** + * 测试附件邮件 + * + * @throws MessagingException 邮件异常 + */ + @Test + public void sendAttachmentsMail() throws MessagingException { + URL resource = ResourceUtil.getResource("static/xkcoding.png"); + mailService.sendAttachmentsMail("237497819@qq.com", "这是一封带附件的邮件", "邮件中有附件,请注意查收!", resource.getPath()); + } + + /** + * 测试静态资源邮件 + * + * @throws MessagingException 邮件异常 + */ + @Test + public void sendResourceMail() throws MessagingException { + String rscId = "xkcoding"; + String content = "这是带静态资源的邮件
"; + URL resource = ResourceUtil.getResource("static/xkcoding.png"); + mailService.sendResourceMail("237497819@qq.com", "这是一封带静态资源的邮件", content, resource.getPath(), rscId); + } +} +``` + +## welcome.html + +> 此文件为邮件模板,位于 resources/templates 目录下 + +```html + + + + + SpringBootDemo(入门SpringBoot的首选Demo) + + + +
+

欢迎使用 - Powered By

+ +
+ + spring-boot-demo,入门Spring Boot的首选Demo!:) + +
+
+ 如果对你有帮助,请任意打赏 +
+
+
+
+
+
+ +
+
微信打赏
+
+
+
+
+
支付宝打赏
+
+
+
+
+ +
+ + +``` + +## 参考 + +- Spring Boot 官方文档:https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-email +- Spring Boot 官方文档:https://docs.spring.io/spring/docs/5.1.2.RELEASE/spring-framework-reference/integration.html#mail \ No newline at end of file diff --git a/spring-boot-demo-email/pom.xml b/spring-boot-demo-email/pom.xml index ae25602..db331bb 100644 --- a/spring-boot-demo-email/pom.xml +++ b/spring-boot-demo-email/pom.xml @@ -1,48 +1,60 @@ - 4.0.0 - - spring-boot-demo-email - 1.0.0-SNAPSHOT - jar - - spring-boot-demo-email - Demo project for Spring Boot - - - com.xkcoding - spring-boot-demo - 1.0.0-SNAPSHOT - - - - UTF-8 - UTF-8 - 1.8 - - - - - org.springframework.boot - spring-boot-starter-mail - - - - org.springframework.boot - spring-boot-starter-test - test - - - - - spring-boot-demo-email - - - org.springframework.boot - spring-boot-maven-plugin - - - + 4.0.0 + + spring-boot-demo-email + 1.0.0-SNAPSHOT + jar + + spring-boot-demo-email + Demo project for Spring Boot + + + com.xkcoding + spring-boot-demo + 1.0.0-SNAPSHOT + + + + UTF-8 + UTF-8 + 1.8 + + + + + + org.springframework.boot + spring-boot-starter-mail + + + + org.springframework.boot + spring-boot-starter-test + test + + + + cn.hutool + hutool-all + + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + + spring-boot-demo-email + + + org.springframework.boot + spring-boot-maven-plugin + + + diff --git a/spring-boot-demo-email/src/main/java/com/xkcoding/email/SpringBootDemoEmailApplication.java b/spring-boot-demo-email/src/main/java/com/xkcoding/email/SpringBootDemoEmailApplication.java index d82bc7f..a02cd8c 100644 --- a/spring-boot-demo-email/src/main/java/com/xkcoding/email/SpringBootDemoEmailApplication.java +++ b/spring-boot-demo-email/src/main/java/com/xkcoding/email/SpringBootDemoEmailApplication.java @@ -19,7 +19,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootDemoEmailApplication { - public static void main(String[] args) { - SpringApplication.run(SpringBootDemoEmailApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(SpringBootDemoEmailApplication.class, args); + } } diff --git a/spring-boot-demo-email/src/main/java/com/xkcoding/email/service/MailService.java b/spring-boot-demo-email/src/main/java/com/xkcoding/email/service/MailService.java new file mode 100644 index 0000000..421d310 --- /dev/null +++ b/spring-boot-demo-email/src/main/java/com/xkcoding/email/service/MailService.java @@ -0,0 +1,65 @@ +package com.xkcoding.email.service; + +import javax.mail.MessagingException; + +/** + *

+ * 邮件接口 + *

+ * + * @package: com.xkcoding.email.service + * @description: 邮件接口 + * @author: yangkai.shen + * @date: Created in 2018/11/21 11:16 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +public interface MailService { + /** + * 发送文本邮件 + * + * @param to 收件人地址 + * @param subject 邮件主题 + * @param content 邮件内容 + * @param cc 抄送地址 + */ + void sendSimpleMail(String to, String subject, String content, String... cc); + + /** + * 发送HTML邮件 + * + * @param to 收件人地址 + * @param subject 邮件主题 + * @param content 邮件内容 + * @param cc 抄送地址 + * @throws MessagingException 邮件发送异常 + */ + void sendHtmlMail(String to, String subject, String content, String... cc) throws MessagingException; + + /** + * 发送带附件的邮件 + * + * @param to 收件人地址 + * @param subject 邮件主题 + * @param content 邮件内容 + * @param filePath 附件地址 + * @param cc 抄送地址 + * @throws MessagingException 邮件发送异常 + */ + void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc) throws MessagingException; + + /** + * 发送正文中有静态资源的邮件 + * + * @param to 收件人地址 + * @param subject 邮件主题 + * @param content 邮件内容 + * @param rscPath 静态资源地址 + * @param rscId 静态资源id + * @param cc 抄送地址 + * @throws MessagingException 邮件发送异常 + */ + void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc) throws MessagingException; + +} \ No newline at end of file diff --git a/spring-boot-demo-email/src/main/java/com/xkcoding/email/service/impl/MailServiceImpl.java b/spring-boot-demo-email/src/main/java/com/xkcoding/email/service/impl/MailServiceImpl.java new file mode 100644 index 0000000..b9e7ec7 --- /dev/null +++ b/spring-boot-demo-email/src/main/java/com/xkcoding/email/service/impl/MailServiceImpl.java @@ -0,0 +1,138 @@ +package com.xkcoding.email.service.impl; + +import cn.hutool.core.util.ArrayUtil; +import com.xkcoding.email.service.MailService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.FileSystemResource; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.stereotype.Service; + +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; +import java.io.File; + +/** + *

+ * 邮件接口 + *

+ * + * @package: com.xkcoding.email.service.impl + * @description: 邮件接口 + * @author: yangkai.shen + * @date: Created in 2018/11/21 13:49 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Service +public class MailServiceImpl implements MailService { + @Autowired + private JavaMailSender mailSender; + @Value("${spring.mail.username}") + private String from; + + /** + * 发送文本邮件 + * + * @param to 收件人地址 + * @param subject 邮件主题 + * @param content 邮件内容 + * @param cc 抄送地址 + */ + @Override + public void sendSimpleMail(String to, String subject, String content, String... cc) { + SimpleMailMessage message = new SimpleMailMessage(); + message.setFrom(from); + message.setTo(to); + message.setSubject(subject); + message.setText(content); + if (ArrayUtil.isNotEmpty(cc)) { + message.setCc(cc); + } + mailSender.send(message); + } + + /** + * 发送HTML邮件 + * + * @param to 收件人地址 + * @param subject 邮件主题 + * @param content 邮件内容 + * @param cc 抄送地址 + * @throws MessagingException 邮件发送异常 + */ + @Override + public void sendHtmlMail(String to, String subject, String content, String... cc) throws MessagingException { + MimeMessage message = mailSender.createMimeMessage(); + MimeMessageHelper helper = new MimeMessageHelper(message, true); + helper.setFrom(from); + helper.setTo(to); + helper.setSubject(subject); + helper.setText(content, true); + if (ArrayUtil.isNotEmpty(cc)) { + helper.setCc(cc); + } + mailSender.send(message); + } + + /** + * 发送带附件的邮件 + * + * @param to 收件人地址 + * @param subject 邮件主题 + * @param content 邮件内容 + * @param filePath 附件地址 + * @param cc 抄送地址 + * @throws MessagingException 邮件发送异常 + */ + @Override + public void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc) throws MessagingException { + MimeMessage message = mailSender.createMimeMessage(); + + MimeMessageHelper helper = new MimeMessageHelper(message, true); + helper.setFrom(from); + helper.setTo(to); + helper.setSubject(subject); + helper.setText(content, true); + if (ArrayUtil.isNotEmpty(cc)) { + helper.setCc(cc); + } + FileSystemResource file = new FileSystemResource(new File(filePath)); + String fileName = filePath.substring(filePath.lastIndexOf(File.separator)); + helper.addAttachment(fileName, file); + + mailSender.send(message); + } + + /** + * 发送正文中有静态资源的邮件 + * + * @param to 收件人地址 + * @param subject 邮件主题 + * @param content 邮件内容 + * @param rscPath 静态资源地址 + * @param rscId 静态资源id + * @param cc 抄送地址 + * @throws MessagingException 邮件发送异常 + */ + @Override + public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc) throws MessagingException { + MimeMessage message = mailSender.createMimeMessage(); + + MimeMessageHelper helper = new MimeMessageHelper(message, true); + helper.setFrom(from); + helper.setTo(to); + helper.setSubject(subject); + helper.setText(content, true); + if (ArrayUtil.isNotEmpty(cc)) { + helper.setCc(cc); + } + FileSystemResource res = new FileSystemResource(new File(rscPath)); + helper.addInline(rscId, res); + + mailSender.send(message); + } +} diff --git a/spring-boot-demo-email/src/main/resources/application.yml b/spring-boot-demo-email/src/main/resources/application.yml index e69de29..6d5b7d1 100644 --- a/spring-boot-demo-email/src/main/resources/application.yml +++ b/spring-boot-demo-email/src/main/resources/application.yml @@ -0,0 +1,15 @@ +spring: + mail: + host: smtp.yeah.net + port: 994 + username: hutool@yeah.net + password: q1w2e3 + protocol: smtp + test-connection: true + default-encoding: UTF-8 + properties: + mail.smtp.auth: true + mail.smtp.starttls.enable: true + mail.smtp.starttls.required: true + mail.smtp.ssl.enable: true + mail.display.sendmail: xkcoding diff --git a/spring-boot-demo-email/src/main/resources/static/xkcoding.png b/spring-boot-demo-email/src/main/resources/static/xkcoding.png new file mode 100644 index 0000000..da90425 Binary files /dev/null and b/spring-boot-demo-email/src/main/resources/static/xkcoding.png differ diff --git a/spring-boot-demo-email/src/main/resources/templates/welcome.html b/spring-boot-demo-email/src/main/resources/templates/welcome.html new file mode 100644 index 0000000..f0b8fa9 --- /dev/null +++ b/spring-boot-demo-email/src/main/resources/templates/welcome.html @@ -0,0 +1,50 @@ + + + + + SpringBootDemo(入门SpringBoot的首选Demo) + + + +
+

欢迎使用 - Powered By

+ +
+ + spring-boot-demo,入门Spring Boot的首选Demo!:) + +
+
+ 如果对你有帮助,请任意打赏 +
+
+
+
+
+
+ +
+
微信打赏
+
+
+
+
+
支付宝打赏
+
+
+
+
+ +
+ + \ No newline at end of file diff --git a/spring-boot-demo-email/src/test/java/com/xkcoding/email/service/MailServiceTest.java b/spring-boot-demo-email/src/test/java/com/xkcoding/email/service/MailServiceTest.java new file mode 100644 index 0000000..8f23dd2 --- /dev/null +++ b/spring-boot-demo-email/src/test/java/com/xkcoding/email/service/MailServiceTest.java @@ -0,0 +1,79 @@ +package com.xkcoding.email.service; + +import cn.hutool.core.io.resource.ResourceUtil; +import com.xkcoding.email.SpringBootDemoEmailApplicationTests; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.thymeleaf.TemplateEngine; +import org.thymeleaf.context.Context; + +import javax.mail.MessagingException; +import java.net.URL; + +/** + *

+ * 邮件测试 + *

+ * + * @package: com.xkcoding.email.service + * @description: 邮件测试 + * @author: yangkai.shen + * @date: Created in 2018/11/21 13:49 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +public class MailServiceTest extends SpringBootDemoEmailApplicationTests { + @Autowired + private MailService mailService; + @Autowired + private TemplateEngine templateEngine; + + /** + * 测试简单邮件 + */ + @Test + public void sendSimpleMail() { + mailService.sendSimpleMail("237497819@qq.com", "这是一封简单邮件", "这是一封普通的SpringBoot测试邮件"); + } + + /** + * 测试HTML邮件 + * + * @throws MessagingException 邮件异常 + */ + @Test + public void sendHtmlMail() throws MessagingException { + Context context = new Context(); + context.setVariable("project", "Spring Boot Demo"); + context.setVariable("author", "yangkai.shen"); + context.setVariable("url", "https://github.com/xkcoding/spring-boot-demo"); + + String emailTemplate = templateEngine.process("welcome", context); + mailService.sendHtmlMail("237497819@qq.com", "这是一封模板HTML邮件", emailTemplate); + } + + /** + * 测试附件邮件 + * + * @throws MessagingException 邮件异常 + */ + @Test + public void sendAttachmentsMail() throws MessagingException { + URL resource = ResourceUtil.getResource("static/xkcoding.png"); + mailService.sendAttachmentsMail("237497819@qq.com", "这是一封带附件的邮件", "邮件中有附件,请注意查收!", resource.getPath()); + } + + /** + * 测试静态资源邮件 + * + * @throws MessagingException 邮件异常 + */ + @Test + public void sendResourceMail() throws MessagingException { + String rscId = "xkcoding"; + String content = "这是带静态资源的邮件
"; + URL resource = ResourceUtil.getResource("static/xkcoding.png"); + mailService.sendResourceMail("237497819@qq.com", "这是一封带静态资源的邮件", content, resource.getPath(), rscId); + } +} \ No newline at end of file