├── .gitignore
├── README.md
├── pom.xml
└── src
├── main
├── java
│ └── com
│ │ └── bpf
│ │ └── springbootmail
│ │ ├── SpringbootMailApplication.java
│ │ └── service
│ │ ├── MailService.java
│ │ └── impl
│ │ └── MailServiceImpl.java
└── resources
│ ├── application.properties
│ └── templates
│ └── emailTemplate.html
└── test
└── java
└── com
└── bpf
└── springbootmail
├── SpringbootMailApplicationTests.java
└── service
└── ServiceTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | /target/
2 | !.mvn/wrapper/maven-wrapper.jar
3 |
4 | ### STS ###
5 | .apt_generated
6 | .classpath
7 | .factorypath
8 | .project
9 | .settings
10 | .springBeans
11 | .sts4-cache
12 |
13 | ### IntelliJ IDEA ###
14 | .idea
15 | *.iws
16 | *.iml
17 | *.ipr
18 |
19 | ### NetBeans ###
20 | /nbproject/private/
21 | /build/
22 | /nbbuild/
23 | /dist/
24 | /nbdist/
25 | /.nb-gradle/
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # springboot2.x发送邮件
2 |
3 | #### 项目介绍
4 | 使用springboot2.x来发送邮件的demo
5 |
6 | https://www.jianshu.com/p/59d15b357201
7 | #### 软件架构
8 | 软件架构说明
9 |
10 |
11 | #### 安装教程
12 |
13 | 1. xxxx
14 | 2. xxxx
15 | 3. xxxx
16 |
17 | #### 使用说明
18 |
19 | 1. xxxx
20 | 2. xxxx
21 | 3. xxxx
22 |
23 | #### 参与贡献
24 |
25 | 1. Fork 本项目
26 | 2. 新建 Feat_xxx 分支
27 | 3. 提交代码
28 | 4. 新建 Pull Request
29 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 | 标签中使用,从而显示图片
33 | */
34 | void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId);
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/src/main/java/com/bpf/springbootmail/service/impl/MailServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.bpf.springbootmail.service.impl;
2 |
3 | import java.io.File;
4 |
5 | import javax.mail.MessagingException;
6 | import javax.mail.internet.MimeMessage;
7 |
8 | import org.slf4j.Logger;
9 | import org.slf4j.LoggerFactory;
10 | import org.springframework.beans.factory.annotation.Autowired;
11 | import org.springframework.beans.factory.annotation.Value;
12 | import org.springframework.core.io.FileSystemResource;
13 | import org.springframework.mail.SimpleMailMessage;
14 | import org.springframework.mail.javamail.JavaMailSender;
15 | import org.springframework.mail.javamail.MimeMessageHelper;
16 | import org.springframework.stereotype.Service;
17 |
18 | import com.bpf.springbootmail.service.MailService;
19 |
20 | @Service
21 | public class MailServiceImpl implements MailService {
22 |
23 | private final Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);
24 |
25 | @Value("${spring.mail.username}")
26 | private String from;
27 |
28 | @Autowired
29 | private JavaMailSender mailSender;
30 | @Override
31 | public void sendSimpleMail(String to, String subject, String content) {
32 |
33 | SimpleMailMessage message = new SimpleMailMessage();
34 | message.setTo(to);//收信人
35 | message.setSubject(subject);//主题
36 | message.setText(content);//内容
37 | message.setFrom(from);//发信人
38 |
39 | mailSender.send(message);
40 | }
41 | @Override
42 | public void sendHtmlMail(String to, String subject, String content) {
43 |
44 | logger.info("发送HTML邮件开始:{},{},{}", to, subject, content);
45 | MimeMessage message = mailSender.createMimeMessage();
46 |
47 | MimeMessageHelper helper;
48 | try {
49 | helper = new MimeMessageHelper(message, true);
50 | helper.setFrom(from);
51 | helper.setTo(to);
52 | helper.setSubject(subject);
53 | helper.setText(content, true);//true代表支持html
54 | mailSender.send(message);
55 | logger.info("发送HTML邮件成功");
56 | } catch (MessagingException e) {
57 | logger.error("发送HTML邮件失败:", e);
58 | }
59 | }
60 | @Override
61 | public void sendAttachmentMail(String to, String subject, String content, String filePath) {
62 |
63 | logger.info("发送带附件邮件开始:{},{},{},{}", to, subject, content, filePath);
64 | MimeMessage message = mailSender.createMimeMessage();
65 |
66 | MimeMessageHelper helper;
67 | try {
68 | helper = new MimeMessageHelper(message, true);
69 | helper.setFrom(from);
70 | helper.setTo(to);
71 | helper.setSubject(subject);
72 | helper.setText(content, true);
73 | FileSystemResource file = new FileSystemResource(new File(filePath));
74 | String fileName = file.getFilename();
75 | helper.addAttachment(fileName, file);//添加附件,可多次调用该方法添加多个附件
76 | mailSender.send(message);
77 | logger.info("发送带附件邮件成功");
78 | } catch (MessagingException e) {
79 | logger.error("发送带附件邮件失败", e);
80 | }
81 |
82 |
83 | }
84 | @Override
85 | public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) {
86 |
87 | logger.info("发送带图片邮件开始:{},{},{},{},{}", to, subject, content, rscPath, rscId);
88 | MimeMessage message = mailSender.createMimeMessage();
89 |
90 | MimeMessageHelper helper;
91 | try {
92 | helper = new MimeMessageHelper(message, true);
93 | helper.setFrom(from);
94 | helper.setTo(to);
95 | helper.setSubject(subject);
96 | helper.setText(content, true);
97 | FileSystemResource res = new FileSystemResource(new File(rscPath));
98 | helper.addInline(rscId, res);//重复使用添加多个图片
99 | mailSender.send(message);
100 | logger.info("发送带图片邮件成功");
101 | } catch (MessagingException e) {
102 | logger.error("发送带图片邮件失败", e);
103 | }
104 | }
105 |
106 | }
107 |
--------------------------------------------------------------------------------
/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.mail.host=smtp.qq.com
2 | spring.mail.username=yourAccount@qq.com
3 | spring.mail.password=yourPassword
4 | spring.mail.default-encoding=UTF-8
5 |
6 | #1、独立微服务
7 | #2、异常处理
8 | #3、定时重试邮件
9 | #4、异步发送
--------------------------------------------------------------------------------
/src/main/resources/templates/emailTemplate.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |