后端依赖
<!--引入mail依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!--使用thymeleaf构建邮件模板-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--验证码-->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
后端配置文件application.yml
spring:
# 关闭thymeleaf缓存
thymeleaf:
cache: false
mail:
host: smtp.163.com
username: 你的邮箱
password: 你的邮箱授权码(163邮箱->设置->开启pop3smtp->获取授权码)
default-encoding: UTF-8
protocol: smtp
properties:
mail:
smtp:
auth: true # 启用SMTP认证
starttls:
enabled: true # 启用SMTP认证
required: true # 必须采用加密链接
后端工具类
package com.test.empback.utils;
import java.io.File;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.mail.internet.MimeMessage;
@Component("mailUtils")
@Slf4j
public class MailUtils {
@Autowired
private MailProperties mailProperties;
@Autowired
private JavaMailSender mailSender;
// 发送简单邮件
public void sendSimpleMall(String to, String subject, String content) {
try {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(mailProperties.getUsername());
message.setTo(to);
message.setCc(mailProperties.getUsername());
message.setSubject(subject);
message.setText(content);
mailSender.send(message);
}
catch(Exception e) {
e.printStackTrace();
}
}
// 发送带附件的邮件
public void sendAttachFileMail(String to, String subject, String content, File file) {
try {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
//邮件发送人
messageHelper.setFrom(mailProperties.getUsername());
//邮件接收人
messageHelper.setTo(to);
//邮件主题
message.setSubject(subject);
//邮件内容
messageHelper.setText(content);
//添加附件
messageHelper.addAttachment(file.getName(), file);
//发送
mailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
// 发送HTML格式文件
public void sendHtmlMail(String to, String subject, String content) {
try {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
//邮件发送人
messageHelper.setFrom(mailProperties.getUsername());
//邮件接收人
messageHelper.setTo(to);
//邮件主题
message.setSubject(subject);
//邮件内容
messageHelper.setText(content, true);
//发送
mailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
后端邮件html模板:这个必须方法resource下边的templates文件夹下
文件名:mailtemplate.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>邮箱验证</title>
<meta charset="utf-8">
</head>
<body>
<!-- 头部 -->
<div style="padding: 10px; background-color: #393D49;">
<h2 style="color: #FFFFFF; margin: 0px;">人事招聘系统</h2>
</div>
<!-- 内容 -->
<div style="padding-top: 10px; padding-bottom: 10px;">
<div style="background-color: snow; padding: 20px;">
<div>
<h3>尊敬的用户:您好!</h3>
<p>说明:您现在正在进行敏感操作,为了确保您的账户安全,我们将通过邮件对您进行身份验证。</p>
<p th:text="${message}"></p>
<div>
<h4>本次的验证码为:</h4>
<div style="background-color: #EBEEF5; padding: 10px;">
<h3 th:text="${code}"></h3>
</div>
<h4>有效期为1分钟</h4>
</div>
<p style="margin-top: 15px;">发件人:人事招聘管理系统</p>
</div>
</div>
</div>
<!-- 页底 -->
<div style="padding: 10px; text-align: center; background-color: #2F4056;">
<p style="margin: 0px; color: #FFFFFF;">Copyright © 2020 <a href="http://localhost:8080/" style="color: #FFFFFF;">人事招聘系统</a> All Rights Reserved.</p>
</div>
</body>
</html>
控制器发送邮件
public void sendCode(String userName,String email) {
try {
// 随机生成验证码
SpecCaptcha captcha = new SpecCaptcha(10, 10, 4);
captcha.setCharType(Captcha.TYPE_ONLY_NUMBER);
// 存入键值对并设置过期时间(1分钟内有效)
redisUtil.set(CommonConstant.PREFIX_PASSFIND_CODE+userName,captcha.text(),CommonConstant.PASSFIND_CODE_EXPIRE_TIME);
String message = "详情:您正在尝试进行找回密码操作,若非是您本人的行为,请忽略!";
// 设置邮件内容
Context context = new Context();
context.setVariable("message", message);
context.setVariable("code", captcha.text());
String mail = templateEngine.process("mailtemplate.html", context);
// 发送邮件
mailUtils.sendHtmlMail(email, "邮箱验证码", mail);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
完事,这就是所有的实现了,最后截个图