在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.效果如图

相关推荐
captain3767 小时前
MyBatis(入门5)其他查询操作
java·数据库·spring boot·mybatis
Novproxy_Z7 小时前
商用代理避坑:如何识别商家拿机房 IP 冒充住宅 IP
java·网络·tcp/ip
文丑颜不良啊8 小时前
LeetCode1481~1500题目解析
java·leetcode
2601_962885728 小时前
quotes 字段全解:涨跌幅、振幅、换手率的口径与单位
java·前端·spring
前端精髓8 小时前
调用openrouter的免费API模型
java·前端·javascript
步行cgn9 小时前
Spring MVC 拦截器 Interceptor 详解:从执行流程到实战应用
java·spring·mvc
文丑颜不良啊9 小时前
LeetCode1381~1400题目解析
java·leetcode
开开心心_Every9 小时前
快速隐藏程序工具自定义快捷键一键切换
java·开发语言·前端·spring·智能手机·word·intellij-idea
萧瑟余晖10 小时前
Spring Batch核心模型与快速上手详解
java·spring·batch
缓慢更新10 小时前
AI 辅助生成单测:从接口定义到用例的流水线
java·服务器·前端·网络·数据库·人工智能