# springboot-email **Repository Path**: SunPeng-3/springboot-email ## Basic Information - **Project Name**: springboot-email - **Description**: 使用pring-boot-starter-mail 发送文本邮件,单个或多个附件邮件,页面邮件,邮件模板,图片静态邮件。 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 2 - **Created**: 2018-11-30 - **Last Updated**: 2022-08-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # springboot-email #### 项目介绍 使用pring-boot-starter-mail 发送文本邮件,单个或多个附件邮件,页面邮件,邮件模板,图片静态邮件。 #### 软件架构 spring boot #### 使用说明 **1. 第一步 pom.xml 添加依赖包** ``` org.springframework.boot spring-boot-starter-mail org.springframework.boot spring-boot-starter-thymeleaf com.alibaba fastjson 1.2.47 ``` **2. 第二步 application.properties 添加配置文件** ``` # 服务器 spring.mail.host=smtp.126.com # 邮箱地址 替换成自己的 spring.mail.username=xxxxx@126.com # 授权密码 替换成自己的 spring.mail.password=xxxx # 编码 spring.mail.default-encoding=utf-8 ``` **3. 第三步:MailService** ``` package com.sunp.email.springbootemail.service; import com.alibaba.fastjson.JSON; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Service; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; import java.util.List; /** * @ClassName MailService * @Description 邮箱发送 * @Author sunp * @Date 2018/11/29 14:29 * @Version 0.0.1 **/ @Service public class MailService { private final Logger logger = LoggerFactory.getLogger(MailService.class); @Value("${spring.mail.username}") private String from ; @Autowired private JavaMailSender mailSender; /** * 发送邮件模板 文本文件 * @param to 发送给谁 * @param subject 主题是什么 * @param content 邮件内容 */ public void sendSimpleEmail(String to,String subject,String content){ logger.info("开始发送发送邮件模板 文本文件:to={},subject={},content={}",to,subject,content); SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); simpleMailMessage.setTo(to); simpleMailMessage.setSubject(subject); simpleMailMessage.setText(content); simpleMailMessage.setFrom(from); mailSender.send(simpleMailMessage); } /** * 发送网页邮件 * @param to * @param subject * @param content */ public void sendHtmlEmail(String to,String subject,String content){ logger.info("开始发送发送网页邮件:to={},subject={},content={}",to,subject,content); MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = null; try { helper = new MimeMessageHelper(message,true); helper.setTo(to); helper.setSubject(subject); helper.setText(content,true); helper.setFrom(from); mailSender.send(message); logger.info("开始发送发送网页邮件成功 " ); } catch (MessagingException e) { logger.error("开始发送发送网页邮件异常:"+e.getMessage()); } } /** * 发送单个附件的邮件 * @param to * @param subject * @param content * @param filePath */ public void sendAttachmentMail(String to,String subject,String content,String filePath) { logger.info("开始发送单个附件的邮件:to={},subject={},content={},filePath={}",to,subject,content,filePath); MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = null; try { helper = new MimeMessageHelper(message,true); helper.setTo(to); helper.setSubject(subject); helper.setText(content,true); helper.setFrom(from); //读取文件 FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = file.getFilename(); helper.addAttachment(fileName,file); mailSender.send(message); logger.info("单个附件的邮件发送成功"); } catch (MessagingException e) { logger.error("单个附件的邮件发送异常:"+e.getMessage()); } } /** * 发送多个附件邮件 * @param to * @param subject * @param content * @param filePaths 文件地址集合 */ public void sendAttachmentsMail(String to,String subject,String content, List filePaths) { logger.info("发送多个附件邮件: to={},subject={},content={},filePath={}",to,subject,content, JSON.toJSON(filePaths)); MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = null; try { helper = new MimeMessageHelper(message,true); helper.setTo(to); helper.setSubject(subject); helper.setText(content,true); helper.setFrom(from); if (filePaths.size()>0){ for (String filePath: filePaths){ //读取文件 FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = file.getFilename(); helper.addAttachment(fileName,file); } } mailSender.send(message); logger.info("发送多个附件邮件成功"); } catch (MessagingException e) { logger.info("发送多个附件邮件异常"+e.getMessage()); } } /** * 发送图片的邮件 * @param to * @param subject * @param content * @param rscPath * @param rscId * @throws MessagingException */ public void sendInlineResourceMail(String to,String subject,String content,String rscPath,String rscId) { logger.info("发送静态邮件开始:to={},subject={},content={},rscPath={},rscId={}",to,subject,content,rscPath,rscId); MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = null; try { helper = new MimeMessageHelper(message,true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content,true); FileSystemResource rsc = new FileSystemResource(new File(rscPath)); helper.addInline(rscId,rsc); logger.info("发送静态图片邮件成功"); } catch (MessagingException e) { logger.error("发送静态邮件异常:"+e.getMessage()); } } } ``` **4. 第四步 写测试类** ``` package com.sunp.email.springbootemail.service; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; import javax.mail.MessagingException; import java.util.ArrayList; import java.util.List; import static org.junit.Assert.*; @RunWith(SpringRunner.class) @SpringBootTest public class MailServiceTest { @Autowired private MailService mailService; @Autowired private TemplateEngine templateEngine; @Test public void sendSimpleEmailTest() { mailService.sendSimpleEmail("494689960@qq.com","测试发送邮件主题","测试邮件发送内容"); } @Test public void sendHtmlEmailTest(){ try { String content = "\n\n" + "

UMeditor," + "简称UM,是为满足广大门户网站对于简单发帖框,或者回复框需求所定制的在线富文本编辑器。" + " UM的主要特点就是容量和加载速度上的改变,主文件的代码量为139k,而且放弃了使用传统的iframe模式," + "采用了div的加载方式, 以达到更快的加载速度和零加载失败率。现在UM的第一个使用者是百度贴吧," + "贴吧每天几亿的pv是对UM各种指标的最好测试平台。 当然随着代码的减少,UM的功能对于UE来说还是有所减少," + "但我们经过调研和大家对于UM提出的各种意见,提供了现在UM的功能版本, 虽然有删减,但也有增加,比如拖拽图片上传," + "chrome的图片拖动改变大小等。让UM能在功能和体积上达到一个平衡。UM还会提供 CDN方式,减少大家部署的成本。" + "我们的目标不仅是要提高在线编辑的编辑体验,也希望能改变前端技术中关于富文本技术的门槛," + "让大家不再觉得这块是个大坑。\n" + "

\n"; mailService.sendHtmlEmail("494689960@qq.com","这是一份Html邮件",content); } catch (MessagingException e) { e.printStackTrace(); } } @Test public void sendAttachmentMailTest() throws MessagingException { String content = "\n\n" + "

UMeditor," + "简称UM,是为满足广大门户网站对于简单发帖框,或者回复框需求所定制的在线富文本编辑器。" + " UM的主要特点就是容量和加载速度上的改变,主文件的代码量为139k,而且放弃了使用传统的iframe模式," + "采用了div的加载方式, 以达到更快的加载速度和零加载失败率。现在UM的第一个使用者是百度贴吧," + "贴吧每天几亿的pv是对UM各种指标的最好测试平台。 当然随着代码的减少,UM的功能对于UE来说还是有所减少," + "但我们经过调研和大家对于UM提出的各种意见,提供了现在UM的功能版本, 虽然有删减,但也有增加,比如拖拽图片上传," + "chrome的图片拖动改变大小等。让UM能在功能和体积上达到一个平衡。UM还会提供 CDN方式,减少大家部署的成本。" + "我们的目标不仅是要提高在线编辑的编辑体验,也希望能改变前端技术中关于富文本技术的门槛," + "让大家不再觉得这块是个大坑。\n" + "

\n"; String filePaht = "C:\\Users\\user\\Desktop\\speedpan_1.9.11.7z"; mailService.sendAttachmentMail("494689960@qq.com","这是一份带附件的邮件",content,filePaht); } @Test public void sendAttachmentsMailTest() throws MessagingException { String content = "\n\n" + "

UMeditor," + "简称UM,是为满足广大门户网站对于简单发帖框,或者回复框需求所定制的在线富文本编辑器。" + " UM的主要特点就是容量和加载速度上的改变,主文件的代码量为139k,而且放弃了使用传统的iframe模式," + "采用了div的加载方式, 以达到更快的加载速度和零加载失败率。现在UM的第一个使用者是百度贴吧," + "贴吧每天几亿的pv是对UM各种指标的最好测试平台。 当然随着代码的减少,UM的功能对于UE来说还是有所减少," + "但我们经过调研和大家对于UM提出的各种意见,提供了现在UM的功能版本, 虽然有删减,但也有增加,比如拖拽图片上传," + "chrome的图片拖动改变大小等。让UM能在功能和体积上达到一个平衡。UM还会提供 CDN方式,减少大家部署的成本。" + "我们的目标不仅是要提高在线编辑的编辑体验,也希望能改变前端技术中关于富文本技术的门槛," + "让大家不再觉得这块是个大坑。\n" + "

\n"; List filePahts = new ArrayList<>(); filePahts.add("C:\\Users\\user\\Desktop\\speedpan_1.9.11.7z"); filePahts.add("C:\\Users\\user\\Desktop\\jdk api 1.8_google.CHM"); mailService.sendAttachmentsMail("494689960@qq.com","这是多份带附件的邮件",content,filePahts); } @Test public void sendInlineResourceMailTest() throws MessagingException { String imgPath = "C:\\Users\\user\\Desktop\\20181129154207.png"; String srcId = "neo001"; String content = " 这里是有图片的邮件:"; mailService.sendInlineResourceMail("494689960@qq.com","这事一封带图片的邮件",content,imgPath,srcId); } // 模板邮件 @Test public void testTemplatesMailTest() throws MessagingException { Context context = new Context(); context.setVariable("id","006"); String emailContent = templateEngine.process("emailTemplates",context); mailService.sendHtmlEmail("494689960@qq.com","这是一个模板邮件",emailContent); } } ``` **5. 申请一个126邮箱开通 设置POP3/SMTP/IMAP** ![输入图片说明](https://images.gitee.com/uploads/images/2018/1130/104655_7975802b_766416.png "屏幕截图.png")