好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring中使用腾讯云发送短信验证码的实现示例

1. 所需依赖

        <dependency>
            <groupId>com.tencentcloudapi</groupId>
            <artifactId>tencentcloud-sdk-java</artifactId>
            <version>3.1.313</version>
        </dependency>

 

2. 腾讯云 配置

 

(1). 获取短信签名

在腾讯云控制台中找到 短信

我使用网站创建签名

需要有域名, 且域名已完成备案

下面这个图是网站备案号, 在腾讯云控制台搜索 网站备案 即可找到

创建成功

记下 SignName

 

(2). 创建正文模板

模板随便选一个即可, 其中的{1} {2}是参数, 后来配置需要

我选择的第一个, 一个参数

成功后, 记下TemplateId

 

(3). 创建密钥

记录密钥 SecredId 和 SecretKey

 

(4). 获取SdkAppId

 

3. 代码

public class SmsServiceTencentSmsImpl {

    public void send(String mobile, String message) { // 参数是电话号码和发送的内容
        try {
            Credential cred = new Credential(你的SecredId, 你的SecredKey);
            // 实例化一个http选项,可选的,没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi测试数据");
            // 实例化一个client选项,可选的,没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            SendSmsRequest req = new SendSmsRequest();
            String[] phoneNumberSet1 = {"+86" + mobile};
            req.setPhoneNumberSet(phoneNumberSet1);

            req.setSmsSdkAppId(你的SdkAppId);
            req.setSignName(你的SignName);
            req.setTemplateId(你的TemplateId);

            String[] templateParamSet1 = {message}; // 你的正文模板参数, 我的是一个, 如果两个数组里两个元素
            req.setTemplateParamSet(templateParamSet1);

            // 返回的resp是一个SendSmsResponse的实例,与请求对象对应
            SendSmsResponse resp = client.SendSms(req);
            // 输出json格式的字符串回包
            System.out.println(SendSmsResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
    }
}

到此这篇关于 Spring 中使用腾讯云发送 短信验证码 的实现示例的文章就介绍到这了,更多相关Spring 腾讯云发送短信验证码内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文地址:https://blog.csdn.net/qq_37354060/article/details/123379559

查看更多关于Spring中使用腾讯云发送短信验证码的实现示例的详细内容...

  阅读:21次