Browse Source

spring-boot-demo-email 完成

pull/1/head
Yangkai.Shen 5 years ago
parent
commit
c141a0ed9d
2 changed files with 82 additions and 0 deletions
  1. +50
    -0
      spring-boot-demo-email/src/main/resources/email/test.html
  2. +32
    -0
      spring-boot-demo-email/src/test/java/com/xkcoding/email/service/MailServiceTest.java

+ 50
- 0
spring-boot-demo-email/src/main/resources/email/test.html View File

@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>SpringBootDemo(入门SpringBoot的首选Demo)</title>
<style>
body {
text-align: center;
margin-left: auto;
margin-right: auto;
}

#welcome {
text-align: center;
}
</style>
</head>
<body>
<div id="welcome">
<h3>欢迎使用 <span th:text="${project}"></span> - Powered By <span th:text=" ${author}"></span></h3>
<span th:text="${url}"></span>
<div style="text-align: center; padding: 10px">
<a style="text-decoration: none;" href="#" th:href="@{${url}}" target="_bank">
<strong>spring-boot-demo,入门Spring Boot的首选Demo!:)</strong>
</a>
</div>
<div style="text-align: center; padding: 4px">
如果对你有帮助,请任意打赏
</div>
<div style="width: 100%;height: 100%;text-align: center;display: flex">
<div style="flex: 1;"></div>
<div style="display: flex;width: 400px;">
<div style="flex: 1;text-align: center;">
<div>
<img width="180px" height="180px" src="http://xkcoding.com/resources/wechat-reward-image.png">
</div>
<div>微信打赏</div>
</div>
<div style="flex: 1;text-align: center;">
<div><img width="180px" height="180px" src="http://xkcoding.com/resources/alipay-reward-image.png">
</div>
<div>支付宝打赏</div>
</div>
</div>
<div style="flex: 1;"></div>
</div>

</div>
</body>
</html>

+ 32
- 0
spring-boot-demo-email/src/test/java/com/xkcoding/email/service/MailServiceTest.java View File

@@ -4,8 +4,13 @@ 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.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;
@@ -28,6 +33,8 @@ public class MailServiceTest extends SpringBootDemoEmailApplicationTests {
private MailService mailService;
@Autowired
private TemplateEngine templateEngine;
@Autowired
private ApplicationContext context;

/**
* 测试简单邮件
@@ -53,6 +60,31 @@ public class MailServiceTest extends SpringBootDemoEmailApplicationTests {
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);
}

/**
* 测试附件邮件
*


Loading…
Cancel
Save