maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
springboot配置文件
# Email
# 发件箱邮件主机地址
spring.mail.host=smtp.qq.com
# 发件人
spring.mail.username=你的QQ邮箱
# 授权码(密码)
spring.mail.password=你开启QQ邮件发送的授权码
具体实现(EmailUtils)
package cn.tcmp.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Map;
@Component
public class EmailUtil {
@Autowired
private JavaMailSender jms;
@Value("${spring.mail.username}")
private String from;
@Autowired
private TemplateEngine templateEngine;
// 发送普通邮件
public void sendEmail(String to, String subject, String context){
MimeMessage mimeMessage = jms.createMimeMessage();
// set邮件对象
MimeMessageHelper helper = null;
try {
helper = new MimeMessageHelper(mimeMessage);
helper.setTo(to); // 收件人
helper.setFrom(from); // 发件人
helper.setSubject(subject); // 标题
helper.setText(context,true); // 内容
// 发邮件
jms.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
/**
* 发附件邮件
* @param to
* @param subject
* @param context
* @param path 附件地址
*/
public void sendEmailXp(String to, String subject, String context,String path){
MimeMessage mimeMessage = jms.createMimeMessage();
MimeMessageHelper helper = null;
try {
// 参数true表示有附件
helper = new MimeMessageHelper(mimeMessage, true);
FileSystemResource fileSystemResource = new FileSystemResource(new File(path));
String fileName = path.substring(0, path.lastIndexOf(File.separator));
// 挂附件 参数一 附件名称 参数二 附件对象
helper.addAttachment(fileName, fileSystemResource);
helper.setTo(to); // 收件人
helper.setFrom(from); // 发件人
helper.setSubject(subject); // 标题
helper.setText(context,true); // 内容 参数true是表示发送的文本市html的
// 发邮件
jms.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
/**
* 发送模板邮件
* @param to
* @param subject
* @param templateName 模板名称
* @param map 给模板中赋值
*/
public void sendEmailXXP(String to, String subject, String templateName, Map<String, Object> map){
MimeMessage mimeMessage = jms.createMimeMessage();
MimeMessageHelper helper = null;
try {
helper = new MimeMessageHelper(mimeMessage);
helper.setTo(to);
helper.setFrom(from); // 发件人
helper.setSubject(subject); // 标题
Context context = new Context();
context.setVariables(map);
// 解析模板为文本 参数1:模板名 参数2:data数据
String text = templateEngine.process(templateName, context);
helper.setText(text, true);
jms.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
controller中的实现(EmailController)
package cn.tcmp.controller;
import cn.tcmp.service.StudentService;
import cn.tcmp.util.EmailUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
@Controller
public class EmailController {
@Resource
private EmailUtil emailUtil;
@Resource
private StudentService studentService;
@ResponseBody
@RequestMapping("testEmail")
public String testEmail(){
String to = "89591124@qq.com";
String subject = "测试bootEmail";
String context = "测你个头";
emailUtil.sendEmail(to, subject, context);
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>");
return "成功了";
}
@ResponseBody
@RequestMapping("testEmailxp")
public String testEmailxp(){
String to = "495381306@qq.com";
String subject = "啦啦啦";
String context = "测你个头啊";
String path = "C:\\Users\\HASEE\\Desktop\\lalal.txt";
emailUtil.sendEmailXp(to, subject, context,path);
System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<");
return "成功了xp";
}
@ResponseBody
@RequestMapping("sendX")
public String sendX(){
String to = "495381306@qq.com";
String subject = "啦啦啦";
String templateName = "emaild.html";
Map<String, Object> map = new HashMap<>();
map.put("username", "学生人数");
map.put("text", studentService.queryStudentCount());
emailUtil.sendEmailXXP(to,subject,templateName,map);
return "成功";
}
}