下拉找到 POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 打开 POP3/SMTP服务,并记住授权码,后面发送邮件时会用到授权码
CREATE TABLE `user` (
`userid` int(20) NOT NULL AUTO_INCREMENT COMMENT '用户编号',
`name` varchar(16) DEFAULT NULL COMMENT '姓名',
`password` varchar(16) DEFAULT '' COMMENT '密码',
`sex` varchar(12) DEFAULT NULL COMMENT '性别',
`idno` varchar(18) DEFAULT NULL COMMENT '身份证号码',
`tel` int(11) DEFAULT NULL COMMENT '手机号码',
`user_verification_code` int(6) DEFAULT NULL COMMENT '验证码',
`user_activation_code` varchar(255) DEFAULT NULL COMMENT '激活码',
`eml` varchar(255) DEFAULT '' COMMENT '邮箱',
`vipid` int(1) DEFAULT 0 COMMENT '会员等级',
`permissionid` int(1) DEFAULT 0 COMMENT '权限等级',
`registerdata` datetime DEFAULT NULL COMMENT '注册日期',
`status` tinyint(1) DEFAULT NULL COMMENT '状态:0 未激活 1激活',
PRIMARY KEY (`userid`)
) ENGINE=InnoDB AUTO_INCREMENT=1035 DEFAULT CHARSET=utf8
<!--引入邮件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
为了使项目能够跑通测试,以下是pom表的所有配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.5.3version>
<relativePath/>
parent>
<groupId>com.demogroupId>
<artifactId>yuyueartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>yuyuename>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
<skipTests>trueskipTests>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.3version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-mailartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<fork>truefork>
configuration>
plugin>
plugins>
build>
project>
package com.demo.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.sql.Timestamp;
@Data //lombok---自动创建get、set等方法
@NoArgsConstructor //lombok---无参构造
@AllArgsConstructor //lombok---全参构造
@Accessors(chain = true) //开启链式编程
@TableName("user") //关联数据表--user表的名字
public class User {
//主键自增
@TableId(type= IdType.AUTO)
private Integer userid; //登录账号
private String name; //姓名
private String password; //密码
private String repassword; //确认密码
private String sex; //性别
private String idno; //身份证号码
private Integer userVerificationCode; //验证码
private Integer userActivationCode; //激活码
private String eml; //邮箱
private String tel; //联系电话
private Integer vipid; //vip标志id
private Integer permissionid; //权限标志id
private boolean status; //状态:0 未激活 1激活
//日期出参格式化
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Timestamp registerdata; //注册时间
@TableField(exist = false) //不是数据表格中固有的属性
private String vipname; //vip标志名称
@TableField(exist = false) //不是数据表格中固有的属性
private String permissionname; //权限标志名称
}
server:
port: 8090
spring:
#连接数据数据库
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/yuyue?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: root
#如果数据库密码以数字0开头 则必须使用""号包裹
#password: "01234"
#连接发送者邮箱
mail:
host: smtp.qq.com #这个是QQ邮箱的,发件人邮箱的 SMTP 服务器地址, 必须准确, 不同邮件服务器地址不同, 一般(只是一般, 绝非绝对)格式为: smtp.xxx.com,可以百度
username: [email protected] #qq邮箱
password: #qq邮箱授权码
protocol: smtp #发送邮件协议
properties.mail.smtp.auth: true #设置是否需要认证,如果为true,那么用户名和密码就必须的,
properties.mail.smtp.starttls.enable: true
properties.mail.smtp.starttls.required: true
properties.mail.smtp.ssl.enable: true #开启SSL
default-encoding: utf-8
#SpringBoot整合MP配置
mybatis-plus:
#定义别名包: 实现对象映射
type-aliases-package: com.demo.pojo
#加载映射文件一个接口对应一个映射文件
mapper-locations: classpath:/mybatis/*.xml
#开启驼峰映射
configuration:
map-underscore-to-camel-case: true
#不打印日志
debug: false
#Mapper接口执行 打印Sql日志
logging:
level:
com.jt.mapper: debug
spring:
mail:
host: smtp.qq.com #发送邮件服务器
username: [email protected] #发送者邮箱
password: xxxxxxxx #发送者邮箱授权码
protocol: smtp #发送邮件协议
properties.mail.smtp.auth: true #开启认证
properties.mail.smtp.port: 994 #设置端口465或者994
properties.mail.display.sendmail: aaa #可以任意
properties.mail.display.sendname: bbb #可以任意
properties.mail.smtp.starttls.enable: true
properties.mail.smtp.starttls.required: true
properties.mail.smtp.ssl.enable: true #开启SSL
default-encoding: utf-8
#from: [email protected] #发送者邮箱
spring.mail.host=smtp.qq.com //这个是QQ邮箱的 其他邮箱请另行百度
spring.mail.username=用户名 //发送方的邮箱
spring.mail.password=密码 //对于qq邮箱而言 密码指的就是发送方的授权码
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.smtp.starttls.enable=true //开启SSL
spring.mail.properties.mail.smtp.starttls.required=true
package com.demo.controller;
import com.demo.pojo.User;
import com.demo.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController//接受请求
@CrossOrigin //解决跨域
@RequestMapping("/email") //访问路径
public class EmailController{
//注入对象
@Autowired
private EmailService emailService;
@PostMapping ("/sendEmail")
public String sendEmail(User user){
System.out.println("发送邮件。。。。");
return emailService.sendEmail(user);
}
@PostMapping ("/verificationEmail")
public String verificationEmail(User user){
System.out.println("验证-邮箱发送的验证码。。。。");
return emailService.verificationEmail(user);
}
}
package com.demo.service;
import com.demo.pojo.User;
public interface EmailService {
//发送验证码
String sendEmail(User user);
}
package com.demo.service;
import com.demo.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import java.util.Random;
@Service
public class EmailServiceImpl implements EmailService {
//定义验证码
private Integer userVerificationCode = null;
@Autowired
JavaMailSender jms;
//读取配置文件邮箱账号参数
@Value("${spring.mail.username}")
private String sender;
//发送验证码
@Override
public String sendEmail(User user) {
//随机数用作验证
Integer userVerificationCode = new Random().nextInt(999999);
try {
//建立邮件消息
SimpleMailMessage mainMessage = new SimpleMailMessage();
//发送者
mainMessage.setFrom(sender);
//接收者
mainMessage.setTo(user.getEml());
//发送的标题
mainMessage.setSubject("邮箱验证");
//发送的内容
String msg = "您好!" + user.getEml() + ",您正在使用邮箱验证,验证码:" + userVerificationCode + "。";
mainMessage.setText(msg);
//发送邮件
jms.send(mainMessage);
//下面是加入缓存,以便于进行邮箱验证
this.userVerificationCode = userVerificationCode;
} catch (Exception e) {
return ("发送邮件失败,请核对邮箱账号");
}
return "验证码已经发送您的邮箱,请前去邮箱查看,验证码是:" + userVerificationCode ;
}
@Override
public String verificationEmail(User user) {
if (this.userVerificationCode.equals(user.getUserVerificationCode())){
return "验证成功";
}
return "验证失败";
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>邮箱验证测试</title>
<script src="../js/jquery-3.6.0.min.js"></script>
<script src="../js/axios.js"></script>
<script>
function register(){
axios.post("http://localhost:8090/fkxinli/register", $("#f1").serialize())
.then(function(result){
console.log(result.data)
})
}
function register1(){
$.ajax({ //发起Ajax请求数据
type: "POST", //POST隐藏请求自带的数据,get显示请求自带的数据
url: "http://localhost:8080/fkxinli/register", //要使用的请求路径
//contentType: "application/json;charset=utf-8",
data:$("#f1").serialize(),
success: function(data) { //成功时的方案
document.write(data);
},
error: function(data) {
//alert("返回失败");
//console.log("注册失败");
}
})
}
function sendEmail(){
$.ajax({ //发起Ajax请求数据
type: "POST", //POST隐藏请求自带的数据,get显示请求自带的数据
url: "http://localhost:8090/email/sendEmail", //要使用的请求路径
//contentType: "application/json;charset=utf-8",
data:$("#f1").serialize(),
success: function(data) { //成功时的方案
alert(data);
},
error: function(data) {
//alert("返回失败");
//console.log("注册失败");
}
})
}
function verificationEmail(){
$.ajax({ //发起Ajax请求数据
type: "POST", //POST隐藏请求自带的数据,get显示请求自带的数据
url: "http://localhost:8090/email/verificationEmail", //要使用的请求路径
//contentType: "application/json;charset=utf-8",
data:$("#f1").serialize(),
success: function(data) { //成功时的方案
alert(data);
},
error: function(data) {
//alert("返回失败");
//console.log("注册失败");
}
})
}
<!--返回首页-->
function returnfrontpage(){
window.open("../1-homepage/frontpage.html")
}
</script>
</head>
<body>
<h1 align="center">邮箱验证测试</h1>
<form id="f1">
<table align="center">
<tr>
<td>电子邮箱:</td>
<td>
<input type="email" name="eml" placeholder="请输入电子邮箱"/>
<input type="button" value="发送验证码" onclick="sendEmail()" />
</td>
</tr>
<tr>
<td>邮箱验证码:</td>
<td>
<input type="text" name="userVerificationCode" placeholder="请输入邮箱验证码"/>
<input type="button" value="验证--邮箱发送的验证码" onclick="verificationEmail()" />
</td>
</tr>
</table>
</form>
</body>
</html>