以下例子用foxmail发送 , QQ和163等其他都一样
1.pom.xml 添加maven坐标(thymeleaf模板. mail邮件)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2.application.yml (host是 smtp.看foxmail图)
spring:
mail:
host: mail.test.com
password: paddy168
username: paddy@test.com
#port: 端口号
default-encoding: UTF-8
properties:
mail.smtp.socketFactory.fallback : true
mail.smtp.starttls.enable: true
3.整个逻辑.
a)test类
package com.dome;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
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.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class Dome1 {
@Autowired
private JavaMailSender mailSender; //自动注入的Bean
@Autowired
private TemplateEngine templateEngine;
@Value("${spring.mail.username}")
private String Sender; //读取配置文件中的参数
<!-- 简单文字-->
@Test
public void sendSimpleMail1() throws Exception {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(Sender);
message.setTo("john@qq.com"); //发送邮
message.setSubject("主题:来自雯雯的问候"+i);
message.setText("小志是个帅哥"+i);
mailSender.send(message);
System.out.println("ok");
}
<!-- 多个收件人, 多个秒抄人 , 内容下带图片 , 带附件-->
@Test
public void sendSimpleMail2()throws Exception{
String[] filePath = new String[]{"D:\\fox.png"};
Map<String, Object> valueMap = new HashMap<>();
valueMap.put("to", new String[]{"john@qq.com", "john1@qq.com"});
valueMap.put("cc",new String[]{"john2@qq.com"});
valueMap.put("title", "标题");
valueMap.put("content", "Dear admin,");
valueMap.put("content1", "这是测试内容...");
valueMap.put("filePathList", filePath);
MimeMessage mimeMessage = null;
try {
mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
// 设置发件人邮箱
helper.setFrom(Sender);
// 设置收件人邮箱
helper.setTo((String[])valueMap.get("to"));
// 抄送邮件接收人
helper.setCc((String[])valueMap.get("cc"));
// 设置邮件标题
helper.setSubject(valueMap.get("title").toString());
// 添加正文(使用thymeleaf模板)mail为名
Context context = new Context();
context.setVariables(valueMap);
String content = this.templateEngine.process("mail", context);
helper.setText(content, true);
if (valueMap.get("filePathList") != null) {
String[] filePathList = (String[]) valueMap.get("filePathList");
for(String f: filePathList) {
FileSystemResource fileSystemResource = new FileSystemResource(new File(f));
// 添加附件
//String fileName = f.substring(f.lastIndexOf(File.separator));
//helper.addAttachment(fileName, fileSystemResource);
// 模板html
// 添加图片(注意代码是 img src="cid:test001")
FileSystemResource file = new FileSystemResource(new File(f));
helper.addInline("test001", file);
}
}
// 发送邮件
mailSender.send(mimeMessage);
System.out.println("发送成功.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
b) mail.html(这是内容模板,参数自己带进来) ,放在 thymeleaf
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div th:text="${content}"></div>
<div th:text="${content1}"></div>
<div>
Thanks
<img src="cid:test001">
</div>
</body>
</html>
-------以上就完成啦. 如有问题 ,请留言哦.谢谢.