QQ邮箱验证,附完整代码,可根据自己需求配置数据库存储

效果图:
在这里插入图片描述

实现代码步骤如下:

  1. 导入POM.XML文件
		<!--mail-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
  1. 获取邮箱授权码
    登录邮箱------->点击设置------->点击账户,下拉------->找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务------->按照步骤开启POP3/SMTP服务 ------->获取授权码
  2. 添加配置项目
#邮箱配置
#平台地址,这里用的是qq邮箱,使用其他邮箱请更换
spring.mail.host = smtp.qq.com
#改成自己的邮箱
spring.mail.username = xxxxx@qq.com
#发送短信后它给你的授权码 填写到这里
#spring.mail.password = xxxxx
spring.mail.password = xxxxx
#这东西不用改
spring.mail.properties.mail.smtp.ssl.enable=true
##编码格式
spring.mail.default-encoding=UTF-8
  1. 关键代码

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.org//DTD Mapper 3.0//EN" "http://mybatis.org/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.com
#改成自己的邮箱
spring.mail.username = xxxxxx@qq.com
#发送短信后它给你的授权码 填写到这里
spring.mail.password = xxxx
#这东西不用改
spring.mail.properties.mail.smtp.ssl.enable=true
##编码格式
spring.mail.default-encoding=UTF-8

进阶完整代码
Java实现QQ邮箱登录,实现邮箱验证码三分钟失效,代码实现发送验证码和登录全过程思路

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XuDream

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值