iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Spring Boot腾讯云短信申请与使用示例
  • 874
分享到

Spring Boot腾讯云短信申请与使用示例

2024-04-02 19:04:59 874人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

1.在pom.xml文件中添加依赖 <dependency> <groupId>com.tencentcloudapi</groupId&

1.在pom.xml文件中添加依赖


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

2.新建SmsConstant常量类保存需要用到的常量


public class SmsConstant {
    public  static final String SDKAPPID = "";
    public  static final String SECRETID = "";
    public  static final String SECRETKEY = "";
    public  static final String SIGNNAME  = "";
    public  static final String TEMPLATEID  = "";
}

下面说明一下以上各个值如何获取

2.1.点击进入腾讯云短信服务官网

在这里插入图片描述
在这里插入图片描述

2.2.点击进入腾讯云短信控制台
SDKAPPID 就是下图中的SDKAppID,没有就创建

在这里插入图片描述

2.3.点击进入 API密钥管理
SECRETID 和SECRETKEY 就是下图中的SecretID 和 SecretKey

在这里插入图片描述

2.4.创建签名,等待审核

在这里插入图片描述

SIGNNAME 就是下图中的内容

在这里插入图片描述

2.5.创建正文模板,等待审核
TEMPLATEID 就是下图中的ID

在这里插入图片描述

3.新建SendSmsUtil工具


import com.admin.common.constant.SmsConstant;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;

//导入可选配置类
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;

// 导入对应SMS模块的client
import com.tencentcloudapi.sms.v20210111.SmsClient;

// 导入要请求接口对应的request response类
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;

public class SendSmsUtil {
    public static void sendSms(String phoneNumber, String verificationCode) {
        try {
            
            Credential cred = new Credential(SmsConstant.SECRETID, SmsConstant.SECRETKEY);

            // 实例化一个http选项,可选,没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            // 设置代理
            // httpProfile.setProxyHost("真实代理ip");
            // httpProfile.setProxyPort(真实代理端口);
            
            httpProfile.setReqMethod("POST");
            
            httpProfile.setConnTimeout(60);
            
            httpProfile.setEndpoint("sms.tencentcloudapi.com");

            
            ClientProfile clientProfile = new ClientProfile();
            
            clientProfile.setSignMethod("HMacSHA256");
            clientProfile.setHttpProfile(httpProfile);
            
            SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
            
            SendSmsRequest req = new SendSmsRequest();

            

            
            String sdkAppId = SmsConstant.SDKAPPID;
            req.setSmsSdkAppId(sdkAppId);

            
            String signName = SmsConstant.SIGNNAME;
            req.setSignName(signName);

            
            String senderid = "";
            req.setSenderId(senderid);

            
            String sessionContext = "xxx";
            req.setSessionContext(sessionContext);

            
            String extendCode = "";
            req.setExtendCode(extendCode);

            
            String templateId = SmsConstant.TEMPLATEID;
            req.setTemplateId(templateId);

            
            String[] phoneNumberSet = {"+86" + phoneNumber};
            req.setPhoneNumberSet(phoneNumberSet);

            
            String[] templateParamSet = {verificationCode};
            req.setTemplateParamSet(templateParamSet);

            
            SendSmsResponse res = client.SendSms(req);

            // 输出JSON格式的字符串回包
            System.out.println(SendSmsResponse.tojsonString(res));

            // 也可以取出单个值,你可以通过官网接口文档或跳转到response对象的定义处查看返回字段的定义
            System.out.println(res.getRequestId());

        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
    }
}

在这里插入图片描述

到此这篇关于Spring Boot腾讯云短信申请与使用的文章就介绍到这了,更多相关spring Boot腾讯云短信内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Spring Boot腾讯云短信申请与使用示例

本文链接: https://www.lsjlt.com/news/159655.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作