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**
