diff --git a/spring-boot-demo-email/README.md b/spring-boot-demo-email/README.md
index 90e39bc..a3bdd47 100644
--- a/spring-boot-demo-email/README.md
+++ b/spring-boot-demo-email/README.md
@@ -8,76 +8,83 @@
- 4.0.0
+ 4.0.0
- spring-boot-demo-email
+ 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
- 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
-
-
-
+
+
+
+ UTF-8
+ UTF-8
+ 1.8
+ 2.1.1
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-mail
+
+
+
+
+ com.github.ulisesbocchio
+ jasypt-spring-boot-starter
+ ${jasypt.version}
+
+
+
+ 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
+ host: smtp.mxhichina.com
+ port: 465
+ username: spring-boot-demo@xkcoding.com
+ # 使用 jasypt 加密密码
+ password: ENC(6XYNBOJrcmAOiNqZiVaqw/ff8rjusN2H)
protocol: smtp
test-connection: true
default-encoding: UTF-8
@@ -86,7 +93,11 @@ spring:
mail.smtp.starttls.enable: true
mail.smtp.starttls.required: true
mail.smtp.ssl.enable: true
- mail.display.sendmail: xkcoding
+ mail.display.sendmail: spring-boot-demo
+# 为 jasypt 配置解密秘钥
+jasypt:
+ encryptor:
+ password: spring-boot-demo
```
## MailService.java
@@ -303,6 +314,8 @@ public class MailServiceTest extends SpringBootDemoEmailApplicationTests {
private MailService mailService;
@Autowired
private TemplateEngine templateEngine;
+ @Autowired
+ private ApplicationContext context;
/**
* 测试简单邮件
@@ -321,13 +334,38 @@ public class MailServiceTest extends SpringBootDemoEmailApplicationTests {
public void sendHtmlMail() throws MessagingException {
Context context = new Context();
context.setVariable("project", "Spring Boot Demo");
- context.setVariable("author", "yangkai.shen");
+ 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);
}
+ /**
+ * 测试HTML邮件,自定义模板目录
+ *
+ * @throws MessagingException 邮件异常
+ */
+ @Test
+ public void sendHtmlMail2() throws MessagingException {
+
+ SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
+ templateResolver.setApplicationContext(context);
+ templateResolver.setCacheable(false);
+ templateResolver.setPrefix("classpath:/email/");
+ templateResolver.setSuffix(".html");
+
+ templateEngine.setTemplateResolver(templateResolver);
+
+ 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("test", context);
+ mailService.sendHtmlMail("237497819@qq.com", "这是一封模板HTML邮件", emailTemplate);
+ }
+
/**
* 测试附件邮件
*
diff --git a/spring-boot-demo-email/pom.xml b/spring-boot-demo-email/pom.xml
index db331bb..49adcb8 100644
--- a/spring-boot-demo-email/pom.xml
+++ b/spring-boot-demo-email/pom.xml
@@ -1,60 +1,68 @@
- 4.0.0
+ 4.0.0
- spring-boot-demo-email
+ 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
- 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
-
-
-
+
+
+
+ UTF-8
+ UTF-8
+ 1.8
+ 2.1.1
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-mail
+
+
+
+
+ com.github.ulisesbocchio
+ jasypt-spring-boot-starter
+ ${jasypt.version}
+
+
+
+ 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/resources/application.yml b/spring-boot-demo-email/src/main/resources/application.yml
index 6d5b7d1..28feaca 100644
--- a/spring-boot-demo-email/src/main/resources/application.yml
+++ b/spring-boot-demo-email/src/main/resources/application.yml
@@ -1,9 +1,10 @@
spring:
mail:
- host: smtp.yeah.net
- port: 994
- username: hutool@yeah.net
- password: q1w2e3
+ host: smtp.mxhichina.com
+ port: 465
+ username: spring-boot-demo@xkcoding.com
+ # 使用 jasypt 加密密码
+ password: ENC(6XYNBOJrcmAOiNqZiVaqw/ff8rjusN2H)
protocol: smtp
test-connection: true
default-encoding: UTF-8
@@ -12,4 +13,8 @@ spring:
mail.smtp.starttls.enable: true
mail.smtp.starttls.required: true
mail.smtp.ssl.enable: true
- mail.display.sendmail: xkcoding
+ mail.display.sendmail: spring-boot-demo
+# 为 jasypt 配置解密秘钥
+jasypt:
+ encryptor:
+ password: spring-boot-demo
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
index 26e4c0e..52b7fd9 100644
--- 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
@@ -8,9 +8,6 @@ import org.springframework.context.ApplicationContext;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
-import org.thymeleaf.templatemode.TemplateMode;
-import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
-import org.thymeleaf.templateresolver.FileTemplateResolver;
import javax.mail.MessagingException;
import java.net.URL;
@@ -108,4 +105,4 @@ public class MailServiceTest extends SpringBootDemoEmailApplicationTests {
URL resource = ResourceUtil.getResource("static/xkcoding.png");
mailService.sendResourceMail("237497819@qq.com", "这是一封带静态资源的邮件", content, resource.getPath(), rscId);
}
-}
\ No newline at end of file
+}