说明
https://gitee.com/xkcoding/spring-boot-demo
此 demo 主要演示了 Spring Boot 如何整合邮件功能,包括发送简单文本邮件、HTML邮件(包括模板HTML邮件)、附件邮件、静态资源邮件
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-demo-email</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>jar</packaging>
<name>spring-boot-demo-email</name> <description>Demo project for Spring Boot</description>
<parent> <groupId>com.xkcoding</groupId> <artifactId>spring-boot-demo</artifactId> <version>1.0.0-SNAPSHOT</version> </parent>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <jasypt.version>2.1.1</jasypt.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>${jasypt.version}</version> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies>
<build> <finalName>spring-boot-demo-email</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
</project>
|
application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| spring: mail: host: smtp.mxhichina.com port: 465 username: spring-boot-demo@xkcoding.com password: ENC(OT0qGOpXrr1Iog1W+fjOiIDCJdBjHyhy) 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: spring-boot-demo
jasypt: encryptor: password: spring-boot-demo
|
MailService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
public interface MailService {
void sendSimpleMail(String to, String subject, String content, String... cc);
void sendHtmlMail(String to, String subject, String content, String... cc) throws MessagingException;
void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc) throws MessagingException;
void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc) throws MessagingException;
}
|
MailServiceImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
@Service public class MailServiceImpl implements MailService { @Autowired private JavaMailSender mailSender; @Value("${spring.mail.username}") private String from;
@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); }
@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); }
@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); }
@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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
public class MailServiceTest extends SpringBootDemoEmailApplicationTests { @Autowired private MailService mailService; @Autowired private TemplateEngine templateEngine; @Autowired private ApplicationContext context;
@Test public void sendSimpleMail() { mailService.sendSimpleMail("237497819@qq.com", "这是一封简单邮件", "这是一封普通的SpringBoot测试邮件"); }
@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); }
@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); }
@Test public void sendAttachmentsMail() throws MessagingException { URL resource = ResourceUtil.getResource("static/xkcoding.png"); mailService.sendAttachmentsMail("237497819@qq.com", "这是一封带附件的邮件", "邮件中有附件,请注意查收!", resource.getPath()); }
@Test public void sendResourceMail() throws MessagingException { String rscId = "xkcoding"; String content = "<html><body>这是带静态资源的邮件<br/><img src=\'cid:" + rscId + "\' ></body></html>"; URL resource = ResourceUtil.getResource("static/xkcoding.png"); mailService.sendResourceMail("237497819@qq.com", "这是一封带静态资源的邮件", content, resource.getPath(), rscId); } }
|
welcome.html
此文件为邮件模板,位于 resources/templates 目录下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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>
|
参考