(已解决)spingboot 后端发送QQ邮箱验证码

打开QQ邮箱pop3请求服务:(按照QQ邮箱引导操作)


导入依赖(不是maven项目就自己添加jar包):

复制代码
<!--        邮件发送-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>3.2.1</version>
        </dependency>

application.properties配置:

:需要yml的,推荐转换工具或者下载idea插件yamls

yml,properties互转工具:yaml和proper互转工具

复制代码
#邮箱配置
spring.mail.host=smtp.qq.com
spring.mail.port=465
spring.mail.username=邮箱地址
spring.mail.password=授权码
spring.mail.test-connection=true
spring.mail.properties.mail.smtp.ssl.enable=true

Controller层:

:我使用的是自己静态验证码,就是为了一步一步来更加清晰,首先是要实现成功发送邮件,

后期大家自己加随机数做动态验证码替换静态验证码就好了,可以使用存入数据库或者redius。

更新(动态验证码发送):动态验证码发送

复制代码
package com.example.tianyidemo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/mail")
@RestController
public class MailDemoController {
    @Autowired
    private JavaMailSender javaMailSender;

    @GetMapping
    public String senderMail() {
        SimpleMailMessage message = new SimpleMailMessage();
        // 发件人 你的邮箱
        message.setFrom("1330141297@qq.com");
        // 接收人 接收者邮箱
        message.setTo(new String[]{"1416655407@qq.com"});

        //邮件标题
        message.setSubject("天易游戏论坛:");

        //邮件内容
        message.setText("尊敬的用户:你好,欢迎使用天易游戏论坛,您的注册验证码为:678253");

        javaMailSender.send(message);

        return "success";
    }
}

EmailService层:

复制代码
package com.example.tianyidemo.service;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

@Service
public class EmailService {
    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    /**
     * 发送邮件
     *
     * @param to      收件人邮箱
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    public void sendMail(String to, String subject, String content) throws MessagingException {
        // 创建邮件消息
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);

        // 发送邮件
        mailSender.send(message);
    }
}

实体类:(不知道有没有用,忘记了,要是报错就加上)

复制代码
@Data
public class Mail {
    public String email;
    public String username;
    public String password;
}

前端测试:(请注意oken的路径拦截!如果没写token可以不用管,也不用请求头添加token)

成功接收

相关推荐
咸鱼の猫36 分钟前
用samba服务器将虚拟机的Ubuntu(磁盘)映射到本地电脑实现文件互传
linux·服务器·ubuntu
Leon-Ning Liu42 分钟前
MySQL 5.7大表索引优化实战:108GB数据建索引效率提升50%
运维·数据库·mysql
wanhengidc44 分钟前
网站服务器具体是指什么
运维·服务器
q***42821 小时前
IPV6公网暴露下的OPENWRT防火墙安全设置(只允许访问局域网中指定服务器指定端口其余拒绝)
服务器·安全·php
人工智能训练1 小时前
前端框架选型破局指南:Vue、React、Next.js 从差异到落地全解析
运维·javascript·人工智能·前端框架·vue·react·next.js
翼龙云_cloud1 小时前
阿里云渠道商:PolarDB如何进行快速恢复?
运维·服务器·阿里云·云计算
工具人55551 小时前
Linux远程登录
linux·运维·服务器
网硕互联的小客服1 小时前
Linux 系统CPU 100% 怎么办?如何处理?
运维·服务器·网络·安全
YJlio2 小时前
进程和诊断工具学习笔记(8.24):Handle——谁占着不放?句柄泄漏排查、强制解锁与检索技巧
服务器·笔记·学习
wangsiling62 小时前
11.13zy
linux·服务器·网络