1.如何发送邮件
1.加入poml依赖
org.springframework.boot
spring-boot-starter-mail
2.配置发送邮件的配置
spring:
mail:
# 配置 SMTP 服务器地址
host: smtp.126.com
# 发送者邮箱
username: [email protected]
# 配置密码,注意不是真正的密码,而是刚刚申请到的授权码
password: FSHJMBSLFDNOBJFL
# 端口号465或587 ,可不写
# port: 587
# 默认的邮件编码为UTF-8
default-encoding: UTF-8
# 配置SSL 加密工厂
properties:
mail:
smtp:
socketFactoryClass: javax.net.ssl.SSLSocketFactory
#表示开启 DEBUG 模式,这样,邮件发送过程的日志会在控制台打印出来,方便排查错误
debug: true
126邮箱SMTP服务器地址:smtp.126.com,端口号:465或者994
2163邮箱SMTP服务器地址:smtp.163.com,端口号:465或者994
yeah邮箱SMTP服务器地址:smtp.yeah.net,端口号:465或者994
qq邮箱SMTP服务器地址:smtp.qq.com,端口号465或587*
3.发送普通邮件
@Autowired
JavaMailSender javaMailSender;
@Test
public void sendSimpleMail() {
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("这是一封测试邮件");
message.setFrom("[email protected]");
message.setTo("[email protected]");
message.setSentDate(new Date());
message.setText("这是测试邮件的正文");
javaMailSender.send(message);
}
4.发送带附件的邮箱
public void sendAttachFileMail() throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setSubject("这是一封测试邮件");
helper.setFrom("79******[email protected]");
helper.setTo("10*****[email protected]");
helper.setSentDate(new Date());
helper.setText("这是测试邮件的正文");
helper.addAttachment("资料.xlsx",new File("/Users/gamedev/Desktop/测试数据 2.xlsx"));
javaMailSender.send(mimeMessage);
}
5,使用模板发送邮件
1.Thymeleaf 作邮件模板,加入依赖
org.springframework.boot
spring-boot-starter-thymeleaf
2.创建Thymeleaf模板:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>hello 欢迎加入 xxx 大家庭,您的入职信息如下:</p>
<table border="1">
<tr>
<td>姓名</td>
<td th:text="${username}"></td>
</tr>
<tr>
<td>工号</td>
<td th:text="${num}"></td>
</tr>
<tr>
<td>薪水</td>
<td th:text="${salary}"></td>
</tr>
</table>
<div style="color: #ff1a0e">一起努力创造辉煌</div>
</body>
</html>
3.发送邮件:
@Autowired
TemplateEngine templateEngine;
@Test
public void sendThymeleafMail() throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setSubject("这是一封测试邮件");
helper.setFrom("[email protected]");
helper.setTo("[email protected]");
helper.setSentDate(new Date());
Context context = new Context();
context.setVariable("username", "javaboy");
context.setVariable("num","000001");
context.setVariable("salary", "99999");
String process = templateEngine.process("hello.html", context);
helper.setText(process,true);
javaMailSender.send(mimeMessage);
}
2.如何读取excel
1.加入阿里巴巴easyexcel依赖
com.alibaba
easyexcel
2.1.6
2.创建excel监听类
@Slf4j
@Service
public class ReadExcelListener extends AnalysisEventListener<StudentInfo> {
private static List<StudentInfo> studentInfoList = new ArrayList<>();
@Override
public void invoke(StudentInfo studentInfo, AnalysisContext analysisContext) {
log.info("------解析到一条数据:{} ------",studentInfo);
studentInfoList.add(studentInfo);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
log.info("------ 读取完毕,开始处理数据 ------");
}
public List<StudentInfo> showStudentInfo(){
log.info("------ 调用数据");
return studentInfoList;
}
}
3.通过controller调用
@Autowired
private ReadExcelListener readExcelListener;
@GetMapping("readExcel")
public String readExcelAndAsyncSendMail(@RequestParam String mail) throws Exception {
log.info("------- 开始读取excel ------");
MailInfo mailInfo = new MailInfo();
mailInfo.setGetPeople(mail);
mailInfo.setTitle("KH88-XXX-异步解析excel成功");
InputStream fis = new FileInputStream("D:\\java\\学生信息.xls");
EasyExcel.read(fis, StudentInfo.class, readExcelListener).sheet().doRead();
List<StudentInfo> studentInfos = readExcelListener.showStudentInfo();
mailInfo.setDetail(studentInfos.toString());
sendMailService.sendMailToTeacher(mailInfo);
log.info("-------结束读取excel ------");
return "发送成功";
}
3.生成excel
1.前面和导出一样
2.写一个静态的资源,以便读取
public class StudentInfoUtil {
public static List<StudentInfo> employees = new ArrayList<>();
static {
employees.add(new StudentInfo(35,"kh88","张三",18,"男","剑四顾海事","2012-04-05","13776571041"));
employees.add(new StudentInfo(36,"kh88","李四",20,"女","剑四顾海事","2012-04-05","13776571041"));
employees.add(new StudentInfo(37,"kh88","王五",21,"男","剑四顾海事","2012-04-05","13776571041"));
}
}
3.controller直接写导出
@GetMapping("writeExcel")
public String writeExcelAndAsyncSendMail(HttpServletResponse response) throws Exception {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("测试", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), StudentInfo.class).sheet("学生信息").doWrite(StudentInfoUtil.employees);
return "写入成功";
}