在SpringBoot中使用AWS SDK实现邮箱验证码服务

1.依赖导入(maven)

复制代码
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>ses</artifactId>
            <version>2.31.46</version>
        </dependency>

2.申请两个key + 发件人邮箱需要验证:

复制代码
aws.aws_access_key_id
复制代码
aws_secret_access_key

3.生成随机6位验证码

java 复制代码
    // 生成随机验证码(6位数字)
    public String generateVerificationCode() {
        return String.format("%06d", new java.util.Random().nextInt(999999));
    }

4.通过构造函数注入依赖,并绑定两个key和region

java 复制代码
private final SesClient sesClient;

public EmailVerificationService(@Value("${cloud.aws.region.static:xx}") String region, @Value("${aws.aws_access_key_id:xx}") String ACCESS_KEY, @Value("${aws_secret_access_key:xx}") String SECRET_KEY) {
        this.sesClient = SesClient.builder()
                .region(Region.of(region))
                .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(ACCESS_KEY, SECRET_KEY)))
                .build();
    }

5.发送验证码

java 复制代码
// 发送验证码邮件
    public void sendVerificationCode(String recipientEmail) {
        String verificationCode = generateVerificationCode();

        // 构建邮件内容
        String subject = "您的验证码";
        String htmlBody = "<h3>您的验证码是: " + verificationCode + "</h3>"
                + "<p>请在5分钟内使用,此验证码有效期10分钟</p>";

        try {
            // 创建邮件请求
            SendEmailRequest request = SendEmailRequest.builder()
                    .source(SENDER_EMAIL)
                    .destination(Destination.builder().toAddresses(recipientEmail).build())
                    .message(Message.builder()
                            .subject(Content.builder().charset("UTF-8").data(subject).build())
                            .body(Body.builder()
                                    .html(Content.builder().charset("UTF-8").data(htmlBody).build())
                                    .build())
                            .build())
                    .build();

            // 发送邮件
            sesClient.sendEmail(request);
            System.out.println("验证码已发送至: " + recipientEmail);

            
        } catch (SesException e) {
            System.err.println("邮件发送失败: " + e.awsErrorDetails().errorMessage());
            throw new RuntimeException("邮件发送失败", e);
        }

6.发完后shutdown

java 复制代码
 finally {
            emailService.shutdown();
        }




    // 关闭客户端
    public void shutdown() {
        sesClient.close();
    }

7.效果如图

相关推荐
咩咩啃树皮5 小时前
第40篇:Vue3组件化开发精讲——组件拆分、复用、父子通信、工程化架构
java·前端·架构
鱟鲥鳚5 小时前
Spring Boot 集成 LangChain4j:从模型调用到 Tool Calling(Demo版)
java·spring boot
大模型码小白7 小时前
【Python零基础教程】继承、多态与魔法函数:面向对象编程三大核心特性详解
java·大数据·开发语言·人工智能·python·ai编程
腾渊信息科技公司7 小时前
Spring Boot对接MES实战:视觉检测数据自动同步方案
java·人工智能·spring boot·后端·计算机视觉·ai·软件需求
爱笑的源码基地8 小时前
高并发 Redis 缓存门诊HIS系统源码,含财务统计药房进销存
java·程序·门诊系统·诊所系统·云诊所源码
wuqingshun3141599 小时前
TCP超时重传机制是为了解决什么问题?
java
莫逸风11 小时前
【AgentScope 2.0】 0. 学习指南
java·llm·agent·agentscope
隔窗听雨眠11 小时前
Spring Boot在云原生时代的编程范式革新研究
spring boot·后端·云原生
z1234567898612 小时前
2026最新两款AI编程工具深度对比实测
java·数据库·ai编程
yaoxin52112312 小时前
470. Java 反射 - Member 接口与 AccessFlag
java·开发语言·python