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);//发送邮件
       
    }
}
相关推荐
gadiaola24 分钟前
【JVM】Java虚拟机(二)——垃圾回收
java·jvm
крон2 小时前
【Auto.js例程】华为备忘录导出到其他手机
开发语言·javascript·智能手机
zh_xuan3 小时前
c++ 单例模式
开发语言·c++·单例模式
coderSong25683 小时前
Java高级 |【实验八】springboot 使用Websocket
java·spring boot·后端·websocket
老胖闲聊3 小时前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot
Blossom.1183 小时前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn
Mr_Air_Boy4 小时前
SpringBoot使用dynamic配置多数据源时使用@Transactional事务在非primary的数据源上遇到的问题
java·spring boot·后端
曹勖之4 小时前
基于ROS2,撰写python脚本,根据给定的舵-桨动力学模型实现动力学更新
开发语言·python·机器人·ros2
豆沙沙包?4 小时前
2025年- H77-Lc185--45.跳跃游戏II(贪心)--Java版
java·开发语言·游戏
军训猫猫头5 小时前
96.如何使用C#实现串口发送? C#例子
开发语言·c#