├── .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 | 4 | 4.0.0 5 | 6 | com.bpf 7 | springboot-mail 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | springboot-mail 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 2.0.5.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-mail 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-thymeleaf 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-test 40 | test 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /src/main/java/com/bpf/springbootmail/SpringbootMailApplication.java: -------------------------------------------------------------------------------- 1 | package com.bpf.springbootmail; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringbootMailApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringbootMailApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/bpf/springbootmail/service/MailService.java: -------------------------------------------------------------------------------- 1 | package com.bpf.springbootmail.service; 2 | 3 | public interface MailService { 4 | /** 5 | * 发送普通文本邮件 6 | * @param to 收件人 7 | * @param subject 主题 8 | * @param content 内容 9 | */ 10 | void sendSimpleMail(String to, String subject, String content); 11 | /** 12 | * 发送HTML邮件 13 | * @param to 收件人 14 | * @param subject 主题 15 | * @param content 内容(可以包含等标签) 16 | */ 17 | void sendHtmlMail(String to, String subject, String content); 18 | /** 19 | * 发送带附件的邮件 20 | * @param to 收件人 21 | * @param subject 主题 22 | * @param content 内容 23 | * @param filePath 附件路径 24 | */ 25 | void sendAttachmentMail(String to, String subject, String content, String filePath); 26 | /** 27 | * 发送带图片的邮件 28 | * @param to 收件人 29 | * @param subject 主题 30 | * @param content 文本 31 | * @param rscPath 图片路径 32 | * @param rscId 图片ID,用于在标签中使用,从而显示图片 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 | 4 | 5 | 邮件模板 6 | 7 | 8 | 您好,感谢您的注册,这是一封验证邮件,请点击下面的链接完成注册,感谢您的支持!
9 | 激活账户 10 | 11 | -------------------------------------------------------------------------------- /src/test/java/com/bpf/springbootmail/SpringbootMailApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.bpf.springbootmail; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringbootMailApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/bpf/springbootmail/service/ServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.bpf.springbootmail.service; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.test.context.SpringBootTest; 7 | import org.springframework.test.context.junit4.SpringRunner; 8 | import org.thymeleaf.TemplateEngine; 9 | import org.thymeleaf.context.Context; 10 | 11 | @RunWith(SpringRunner.class) 12 | @SpringBootTest 13 | public class ServiceTest { 14 | 15 | @Autowired 16 | private MailService mailService; 17 | @Autowired 18 | private TemplateEngine templateEngine; 19 | 20 | /** 21 | * 发送简单纯文本邮件 22 | */ 23 | @Test 24 | public void sendSimpleMail() { 25 | mailService.sendSimpleMail("receiver@email.com", "发送邮件测试", "大家好,这是我用springboot进行发送邮件测试"); 26 | } 27 | 28 | /** 29 | * 发送HTML邮件 30 | */ 31 | @Test 32 | public void sendHtmlMail() { 33 | String content = "

" + "大家好,这是springboot发送的HTML邮件" + "

"; 34 | mailService.sendHtmlMail("receiver@email.com", "发送邮件测试", content); 35 | } 36 | 37 | /** 38 | * 发送带附件的邮件 39 | */ 40 | @Test 41 | public void sendAttachmentMail() { 42 | String content = "

" + "大家好,这是springboot发送的HTML邮件,有附件哦" + "

"; 43 | String filePath = "your file path"; 44 | mailService.sendAttachmentMail("receiver@email.com", "发送邮件测试", content, filePath); 45 | } 46 | 47 | /** 48 | * 发送带图片的邮件 49 | */ 50 | @Test 51 | public void sendInlineResourceMail() { 52 | String rscPath = "your picture path"; 53 | String rscId = "skill001"; 54 | String content = "

" + "大家好,这是springboot发送的HTML邮件,有图片哦" + "

" 55 | + ""; 56 | mailService.sendInlineResourceMail("receiver@email.com", "发送邮件测试", content, rscPath, rscId); 57 | } 58 | 59 | /** 60 | * 指定模板发送邮件 61 | */ 62 | @Test 63 | public void testTemplateMail() { 64 | //向Thymeleaf模板传值,并解析成字符串 65 | Context context = new Context(); 66 | context.setVariable("id", "001"); 67 | String emailContent = templateEngine.process("emailTemplate", context); 68 | 69 | mailService.sendHtmlMail("receiver@email.com", "这是一个模板文件", emailContent); 70 | } 71 | } 72 | --------------------------------------------------------------------------------