java实现发送验证码通过qq邮箱方式

前期准备

要想实现qq邮箱发送,我们就要开启该服务,获得授权。

1、打开qq邮箱来的账户与安全页面。

2、来到账户与安全页面后,进入安全设置。

3、找到SMTP/IMAP服务,我们开启服务。

4、开启服务后,我们会获得一个授权码,保存好该授权码,在Java程序中我们会对其进行一个配置。

在Java中进行一个配置(这里我是以springboot框架中的示例)

1、先对application.yml中进行配置,最主要的是我们要注意配置参数的层级关系,千万不能出错,因为我第一次进行配置时就是层级关系出错了,走了许多弯路。

复制代码
spring:
    mail:
     host: smtp.qq.com
     port: 465   # 使用SSL连接的端口
     username: 发送方的请求账号  # 确保使用完整的邮箱地址
     password: 自己的授权码 # 授权码
     default-encoding: utf-8
     properties:
       mail:
         smtp:
           auth: true
           ssl:
            enable: true  # 启用SSL
           starttls:
            enable: true
            required: true

2、接下来就是在需要的地方写代码

首先注入一个JavaMailSender对象

复制代码
@Autowired
    private JavaMailSender mailSender;

下面实现发送的代码

复制代码
 //2.向邮箱发送验证码(集成邮件功能
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("发送的qq邮箱");//发送的qq邮箱
        message.setTo("接收邮箱的qq");//接收邮件的qq邮箱
        message.setSubject("主题:注册验证码");
        //随机生成6位数字验证码
        Random random = new Random();
        int randomNum = random.nextInt(1000000);
        String randomCode = String.format("%06d", randomNum);
        System.out.println(randomNum);
        message.setText(randomCode);
        mailSender.send(message);//发送邮件

下面时整体代码,使用时调用代码即可

复制代码
package com.ffyc.news.util;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/*
发送qq邮箱
 */
@Component
public class SendEmailUtil {
    @Autowired
    private JavaMailSender mailSender;

   

    public  void sendEmail(String account){
        System.out.println(account);
        //2.向邮箱发送验证码(集成邮件功能
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("发送的qq邮箱");//发送的qq邮箱
        message.setTo(account);//接收邮件的qq邮箱
        message.setSubject("主题:注册验证码");
        //随机生成6位数字验证码
        Random random = new Random();
        int randomNum = random.nextInt(1000000);
        String randomCode = String.format("%06d", randomNum);
        System.out.println(randomNum);
        message.setText(randomCode);
        mailSender.send(message);//发送邮件
       
    }
}
相关推荐
badhope26 分钟前
Mobile-Skills:移动端技能可视化的创新实践
开发语言·人工智能·git·智能手机·github
码云数智-园园2 小时前
微服务架构下的分布式事务:在一致性与可用性之间寻找平衡
开发语言
C++ 老炮儿的技术栈2 小时前
volatile使用场景
linux·服务器·c语言·开发语言·c++
hz_zhangrl2 小时前
CCF-GESP 等级考试 2026年3月认证C++一级真题解析
开发语言·c++·gesp·gesp2026年3月·gespc++一级
大阿明2 小时前
Spring Boot(快速上手)
java·spring boot·后端
Liu628882 小时前
C++中的工厂模式高级应用
开发语言·c++·算法
bearpping2 小时前
Java进阶,时间与日期,包装类,正则表达式
java
IT猿手2 小时前
基于控制障碍函数的多无人机编队动态避障控制方法研究,MATLAB代码
开发语言·matlab·无人机·openclaw·多无人机动态避障路径规划·无人机编队
邵奈一2 小时前
清明纪念·时光信笺——项目运行指南
java·实战·项目
AI科技星2 小时前
全尺度角速度统一:基于 v ≡ c 的纯推导与验证
c语言·开发语言·人工智能·opencv·算法·机器学习·数据挖掘