java案例

您所在的位置:网站首页 任天堂账号注册qq邮箱收不到验证码 java案例

java案例

2023-09-04 09:20| 来源: 网络整理| 查看: 265

java案例----用户注册—发送邮件并激活/发送邮件验证码 一、前期准备 1、准备两个邮箱账号(一个发邮件,一个收邮件) 1.1)登录需要发送邮件的QQ邮箱,找到设置项 1.2)然后在账户栏下,找到(POP3/SMTP)服务协议

在这里插入图片描述

1.3)生成授权码

下拉找到 POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 打开 POP3/SMTP服务,并记住授权码,后面发送邮件时会用到授权码 在这里插入图片描述

二、项目 1、准备用户数据表 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

在这里插入图片描述

2、idea 创建项目 2.1)在项目的pom表中导入邮件jar包 org.springframework.boot spring-boot-starter-mail

为了使项目能够跑通测试,以下是pom表的所有配置

4.0.0 org.springframework.boot spring-boot-starter-parent 2.5.3 com.demo yuyue 0.0.1-SNAPSHOT yuyue Demo project for Spring Boot 1.8 true org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-devtools true org.projectlombok lombok mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-jdbc com.baomidou mybatis-plus-boot-starter 3.4.3 org.springframework.boot spring-boot-starter-mail org.springframework.boot spring-boot-maven-plugin true 2.2)创建user类—用户类 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; //权限标志名称 } 2.3)创建配置文件 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 2.3.1)邮件的配置文件,application.yml写法 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] #发送者邮箱 2.3.2)邮件的配置文件,application.properties写法 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 2.4)创建EmailController类 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); } } 2.5)创建EmailService 类 package com.demo.service; import com.demo.pojo.User; public interface EmailService { //发送验证码 String sendEmail(User user); } 2.6)创建EmailServiceImpl 类 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 "验证失败"; } } 3、准备网页 邮箱验证测试 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") } 邮箱验证测试 电子邮箱: 邮箱验证码: 4、测试

后端代码,写的比较简单,仅仅测试邮箱是否能够发送验证码 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3