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

您所在的位置:网站首页 qq邮箱登录要验证吗 QQ邮箱验证,附完整代码,可根据自己需求配置数据库存储

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

2024-07-14 03:41| 来源: 网络整理| 查看: 265

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

实现代码步骤如下:

导入POM.XML文件 org.springframework.boot spring-boot-starter-mail 获取邮箱授权码 登录邮箱------->点击设置------->点击账户,下拉------->找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务------->按照步骤开启POP3/SMTP服务 ------->获取授权码添加配置项目 #邮箱配置 #平台地址,这里用的是qq邮箱,使用其他邮箱请更换 spring.mail.host = smtp.qq.com #改成自己的邮箱 spring.mail.username = [email protected] #发送短信后它给你的授权码 填写到这里 #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; /** *

* 邮件信息 前端控制器 *

* * @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; /** *

* 邮件信息 服务类 *

* * @author mengdongxu * @since 2021-12-10 */ public interface EmailInfoService extends IService { 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; /** *

* 邮件信息 服务实现类 *

* * @author mengdongxu * @since 2021-12-10 */ @Service public class EmailInfoServiceImpl extends ServiceImpl 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


【本文地址】


今日新闻


推荐新闻


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