admin管理员组文章数量:1566354
效果图:
实现代码步骤如下:
- 导入POM.XML文件
<!--mail-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
- 获取邮箱授权码
登录邮箱------->点击设置------->点击账户,下拉------->找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务------->按照步骤开启POP3/SMTP服务 ------->获取授权码 - 添加配置项目
#邮箱配置
#平台地址,这里用的是qq邮箱,使用其他邮箱请更换
spring.mail.host = smtp.qq
#改成自己的邮箱
spring.mail.username = xxxxx@qq
#发送短信后它给你的授权码 填写到这里
#spring.mail.password = xxxxx
spring.mail.password = xxxxx
#这东西不用改
spring.mail.properties.mail.smtp.ssl.enable=true
##编码格式
spring.mail.default-encoding=UTF-8
- 关键代码
try {
SimpleMailMessage mailMessage = new SimpleMailMessage();
//主题
mailMessage.setSubject(“验证码邮件”);
//生成随机数
String code = randomCode();
//内容
mailMessage.setText(“您收到的验证码是:” + code);
System.out.println(“您收到的验证码是:” + code);
//发给谁
mailMessage.setTo(“收件邮箱”);
//你自己的邮箱(可以去配置文件中获取)
mailMessage.setFrom(“你自己的邮箱”);
//发送
mailSender.send(mailMessage);
} catch (Exception e) {
e.printStackTrace();
return false;
}
完整的代码如下:
备注:可以根据代码自动生成生成各个文件,mybatis-plus根据Mysql数据库自动生成代码到项目
项目结构:
controller层:
import com.thymeleaf.thymeleafstudy.entity.EmailInfo;
import com.thymeleaf.thymeleafstudy.service.EmailInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>
* 邮件信息 前端控制器
* </p>
*
* @author mengdongxu
* @since 2021-12-10
*/
@RestController
@RequestMapping("/emailInfo")
public class EmailInfoController {
@Autowired
EmailInfoService emailInfoService;
public String send(@RequestBody EmailInfo emailInfo){
return emailInfoService.send(emailInfo);
}
}
service层:
package com.thymeleaf.thymeleafstudy.service;
import com.thymeleaf.thymeleafstudy.entity.EmailInfo;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* <p>
* 邮件信息 服务类
* </p>
*
* @author mengdongxu
* @since 2021-12-10
*/
public interface EmailInfoService extends IService<EmailInfo> {
String send(EmailInfo emailInfo);
}
serviceimpl层:
import com.thymeleaf.thymeleafstudy.entity.EmailInfo;
import com.thymeleaf.thymeleafstudy.mapper.EmailInfoMapper;
import com.thymeleaf.thymeleafstudy.service.EmailInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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;
/**
* <p>
* 邮件信息 服务实现类
* </p>
*
* @author mengdongxu
* @since 2021-12-10
*/
@Service
public class EmailInfoServiceImpl extends ServiceImpl<EmailInfoMapper, EmailInfo> implements EmailInfoService {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from;
@Override
public String send(EmailInfo emailInfo) {
try {
SimpleMailMessage mailMessage = new SimpleMailMessage();
//主题
mailMessage.setSubject("验证码邮件");
//生成随机数
String code = randomCode();
//内容
mailMessage.setText("您收到的验证码是:" + code);
System.out.println("您收到的验证码是:" + code);
//发给谁
mailMessage.setTo(emailInfo.getToEmail());
//你自己的邮箱(可以去配置文件中获取)
mailMessage.setFrom(from);
//发送
mailSender.send(mailMessage);
} catch (Exception e) {
e.printStackTrace();
return "异常啦";
}
return "发送成功";
}
/**
* 随机生成6位数的虚拟银行卡
* @return String code
*/
public String randomCode(){
StringBuilder str = new StringBuilder();
Random random = new Random();
for (int i = 0; i < 6; i++) {
str.append(random.nextInt(10));
}
return str.toString();
}
}
mapper层:
import com.thymeleaf.thymeleafstudy.entity.EmailInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* <p>
* 邮件信息 Mapper 接口
* </p>
*
* @author mengdongxu
* @since 2021-12-10
*/
public interface EmailInfoMapper extends BaseMapper<EmailInfo> {
}
mapper.xml层
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis//DTD Mapper 3.0//EN" "http://mybatis/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.thymeleaf.thymeleafstudy.mapper.EmailInfoMapper">
</mapper>
entity实体(备注:我引入了lombok的jar,加了@Data注解,否则自己写Get,Set方法)
package com.thymeleaf.thymeleafstudy.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import java.time.LocalDate;
import java.time.LocalDateTime;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* <p>
* 邮件信息
* </p>
*
* @author mengdongxu
* @since 2021-12-10
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class EmailInfo implements Serializable {
private static final long serialVersionUID=1L;
@TableId(value = "id", type = IdType.ASSIGN_ID)
private String id;
/**
* 内容
*/
private String title;
/**
* 邮箱收件地址
*/
private String toEmail;
}
application.properties配置文件
#邮箱配置
#平台地址,这里用的是qq邮箱,使用其他邮箱请更换
spring.mail.host = smtp.qq
#改成自己的邮箱
spring.mail.username = xxxxxx@qq
#发送短信后它给你的授权码 填写到这里
spring.mail.password = xxxx
#这东西不用改
spring.mail.properties.mail.smtp.ssl.enable=true
##编码格式
spring.mail.default-encoding=UTF-8
进阶完整代码
Java实现QQ邮箱登录,实现邮箱验证码三分钟失效,代码实现发送验证码和登录全过程思路
版权声明:本文标题:QQ邮箱验证,附完整代码,可根据自己需求配置数据库存储 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1726906710a1089854.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论