tencent cloud

短信

动态与公告
公告
产品简介
常用概念
产品概述
审核标准
使用须知
签名审核标准
正文模板审核标准
正文模板示例
审核驳回原因
国际/港澳台短信
国际/港澳台短信简介
国际/港澳台短信购买指南
国际/港澳台短信快速入门
控制台指南
控制台说明
国际/港澳台短信
营销管理
业务统计
应用管理
通用管理
访问管理
实践教程
如何群发祝福短信
如何实现短信验证码功能
API 文档
History
Introduction
API Category
Making API Requests
Status Pull APIs
SMS Statistics APIs
SMS Signature APIs
SMS Delivery APIs
SMS Mobile Number APIs
SMS Conversion Rate APIs
SMS Template APIs
Data Types
Error Codes
短信回调相关接口
SDK 文档
SDK 下载
Java SDK
PHP SDK
Python SDK
Node.js SDK
C# SDK
Go SDK
C++ SDK
常见问题
费用相关问题
安全相关问题
签名相关问题
模板相关问题
发送内容相关问题
其他问题
相关协议
腾讯云短信数据处理和安全协议
腾讯云短信隐私政策
Service Level Agreement
腾讯云短信服务条款
联系我们

C++ SDK

PDF
聚焦模式
字号
最后更新时间: 2024-06-27 15:48:01
SDK 3.0是云 API 3.0平台的配套工具,您可以通过 SDK 使用所有 短信 API。新版 SDK 实现了统一化,具有各个语言版本的 SDK 使用方法相同,接口调用方式相同,错误码相同以及返回包格式相同等优点。
注意:
接入国际站要求的接口版本:
接入国际站需要使用短信 API 2021-01-11 版本,可参考示例代码。
发送短信相关接口:
一次群发请求最多支持200个号码。
签名、正文模板相关接口:
个人认证用户不支持使用签名、正文模板相关接口,只能通过短信控制台 管理短信签名管理短信正文模板。如需使用该类接口,请将 “个人认证” 变更为 “企业认证”,具体操作请参见 实名认证变更指引

前提条件

了解 地域 概念,选择需求的地域接入。
已开通短信服务,创建签名和模板并通过审核,具体操作请参见 国际/港澳台短信快速入门
已在访问管理控制台 > API 密钥管理 页面获取 SecretID 和 SecretKey。
SecretID 用于标识 API 调用者的身份。
SecretKey 用于加密签名字符串和服务器端验证签名字符串的密钥,SecretKey 需妥善保管,避免泄露
短信的调用地址为sms.tencentcloudapi.com

相关资料

各个接口及其参数的详细介绍请参见 API 文档
下载 SDK 源码请访问 Github 仓库 或者 Gitee 仓库

安装 SDK

环境依赖

Github 仓库参见:环境依赖
Gitee 仓库参见:环境依赖

从源代码构建 SDK

Github 仓库参见:从源代码构建 SDK
Gitee 仓库参见:从源代码构建 SDK

示例代码

说明:
所有示例代码仅作参见,无法直接编译和运行,需根据实际情况进行修改,您也可以根据实际需求使用 API 3.0 Explorer 自动化生成 Demo 代码。
每个接口都有一个对应的 Request 结构和一个 Response 结构。本文仅列举几个常用功能的示例代码,如下所示。

发送短信

#include <tencentcloud/core/TencentCloud.h>
#include <tencentcloud/core/profile/HttpProfile.h>
#include <tencentcloud/core/profile/ClientProfile.h>
#include <tencentcloud/core/Credential.h>
#include <tencentcloud/core/NetworkProxy.h>
#include <tencentcloud/core/AsyncCallerContext.h>
#include <tencentcloud/sms/v20210111/SmsClient.h>
#include <tencentcloud/sms/v20210111/model/SendSmsRequest.h>
#include <tencentcloud/sms/v20210111/model/SendSmsResponse.h>

#include <cstdlib>
#include <iostream>
#include <string>

using namespace TencentCloud;
using namespace TencentCloud::Sms::V20210111;
using namespace TencentCloud::Sms::V20210111::Model;
using namespace std;

int main()
{
TencentCloud::InitAPI();

// use the sdk
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey, 切勿将密钥泄露给他人
// 硬编码密钥到代码中有可能随代码泄露而暴露,有安全隐患,并不推荐。
// 为了保护密钥安全,建议将密钥设置在环境变量中或者配置文件中。
// 前往 https://console.tencentcloud.com/cam/capi 获取 API 密钥 SecretId SecretKey
Credential cred = Credential(getenv("TENCENTCLOUD_SECRET_ID"),
getenv("TENCENTCLOUD_SECRET_KEY"));

// 实例化一个http选项,可选的,没有特殊需求可以跳过。
HttpProfile httpProfile = HttpProfile();
httpProfile.SetKeepAlive(true); // 状态保持,默认是False
httpProfile.SetEndpoint("sms.tencentcloudapi.com"); // 指定接入地域域名(默认就近接入)
httpProfile.SetReqTimeout(10); // 请求超时时间,单位为秒(默认60秒)
httpProfile.SetConnectTimeout(10); // 响应超时时间,单位是秒(默认是60秒)

ClientProfile clientProfile = ClientProfile(httpProfile);

SendSmsRequest req = SendSmsRequest();

/* 帮助链接:
* 短信控制台: https://console.tencentcloud.com/smsv2
*/
/* 短信应用 ID: 在 [短信控制台] 添加应用后生成的实际 SdkAppId,例如2400006666 */
req.SetSmsSdkAppId("2400006666");

/* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名 */
req.SetSignName("Tencent");

/* 模板 ID: 必须填写已审核通过的模板 ID */
req.SetTemplateId("449739");

/* 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,若无模板参数,则设置为空 */
req.SetTemplateParamSet(std::vector<std::string>{"1234"});

/* 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]
* 示例如:+60198890000, 其中前面有一个+号 ,60为国家码,198890000为手机号,最多不要超过200个手机号 */
req.SetPhoneNumberSet(std::vector<std::string>{"+60198890000"});

/* 用户的 session 内容(无需要可忽略): 可以携带用户侧 ID 等上下文信息,server 会原样返回 */
req.SetSessionContext("");

/* 短信码号扩展号(无需要可忽略): 默认未开通,如需开通请提交工单*/
req.SetExtendCode("");

/* 国际/港澳台短信 senderid(无需要可忽略): 国内短信填空,默认未开通,如需开通请提交工单 */
req.SetSenderId("");

/* 实例化要请求产品(以sms为例)的client对象
* 第二个参数是地域信息,可以直接填写字符串ap-singapore,支持的地域列表参考 https://www.tencentcloud.com/document/api/382/40466?lang=en#region-list */
SmsClient sms_client = SmsClient(cred, "ap-singapore", clientProfile);

// 设置代理(无需要直接忽略)
// NetworkProxy proxy = NetworkProxy(NetworkProxy::Type::HTTP, "localhost.proxy.com", 8080);
// sms_client.SetNetworkProxy(proxy);

auto outcome = sms_client.SendSms(req);
if (!outcome.IsSuccess())
{
cout << outcome.GetError().PrintAll() << endl;
TencentCloud::ShutdownAPI();
return -1;
}
SendSmsResponse rsp = outcome.GetResult();
cout<<"RequestId="<<rsp.GetRequestId()<<endl;
cout<<"SendSmsResponse="<<rsp.ToJsonString()<<endl;

TencentCloud::ShutdownAPI();

/* 当出现以下错误码时,快速解决方案参考
* [FailedOperation.SignatureIncorrectOrUnapproved](https://www.tencentcloud.com/document/product/382/9558)
* [FailedOperation.TemplateIncorrectOrUnapproved](https://www.tencentcloud.com/document/product/382/9558)
* [UnauthorizedOperation.SmsSdkAppIdVerifyFail](https://www.tencentcloud.com/document/product/382/9558)
* [UnsupportedOperation.ContainDomesticAndInternationalPhoneNumber](https://www.tencentcloud.com/document/product/382/9558)
*/

return 0;
}

拉取回执状态

#include <tencentcloud/core/TencentCloud.h>
#include <tencentcloud/core/profile/HttpProfile.h>
#include <tencentcloud/core/profile/ClientProfile.h>
#include <tencentcloud/core/Credential.h>
#include <tencentcloud/core/NetworkProxy.h>
#include <tencentcloud/core/AsyncCallerContext.h>
#include <tencentcloud/sms/v20210111/SmsClient.h>
#include <tencentcloud/sms/v20210111/model/PullSmsSendStatusRequest.h>
#include <tencentcloud/sms/v20210111/model/PullSmsSendStatusResponse.h>

#include <cstdlib>
#include <iostream>
#include <string>

using namespace TencentCloud;
using namespace TencentCloud::Sms::V20210111;
using namespace TencentCloud::Sms::V20210111::Model;
using namespace std;

int main()
{
TencentCloud::InitAPI();

// use the sdk
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey, 切勿将密钥泄露给他人
// 硬编码密钥到代码中有可能随代码泄露而暴露,有安全隐患,并不推荐。
// 为了保护密钥安全,建议将密钥设置在环境变量中或者配置文件中。
// 前往 https://console.tencentcloud.com/cam/capi 获取 API 密钥 SecretId SecretKey
Credential cred = Credential(getenv("TENCENTCLOUD_SECRET_ID"),
getenv("TENCENTCLOUD_SECRET_KEY"));

// 实例化一个http选项,可选的,没有特殊需求可以跳过。
HttpProfile httpProfile = HttpProfile();
httpProfile.SetKeepAlive(true); // 状态保持,默认是False
httpProfile.SetEndpoint("sms.tencentcloudapi.com"); // 指定接入地域域名(默认就近接入)
httpProfile.SetReqTimeout(30); // 请求超时时间,单位为秒(默认60秒)
httpProfile.SetConnectTimeout(30); // 响应超时时间,单位是秒(默认是60秒)

ClientProfile clientProfile = ClientProfile(httpProfile);

PullSmsSendStatusRequest req = PullSmsSendStatusRequest();

/* 帮助链接:
* 短信控制台: https://console.tencentcloud.com/smsv2
*/
/* 短信应用 ID: 在 [短信控制台] 添加应用后生成的实际 SdkAppId,例如2400006666 */
req.SetSmsSdkAppId("2400006666");
// 设置拉取最大条数,最多100条
req.SetLimit(100);

SmsClient sms_client = SmsClient(cred, "ap-singapore", clientProfile);

// set proxy
// NetworkProxy proxy = NetworkProxy(NetworkProxy::Type::HTTP, "localhost.proxy.com", 8080);
// cvm_client.SetNetworkProxy(proxy);

auto outcome = sms_client.PullSmsSendStatus(req);
if (!outcome.IsSuccess())
{
cout << outcome.GetError().PrintAll() << endl;
TencentCloud::ShutdownAPI();
return -1;
}
PullSmsSendStatusResponse rsp = outcome.GetResult();
cout<<"RequestId="<<rsp.GetRequestId()<<endl;
cout<<"PullSmsSendStatusResponse="<<rsp.ToJsonString()<<endl;

TencentCloud::ShutdownAPI();

return 0;
}

统计短信发送数据

#include <tencentcloud/core/TencentCloud.h>
#include <tencentcloud/core/profile/HttpProfile.h>
#include <tencentcloud/core/profile/ClientProfile.h>
#include <tencentcloud/core/Credential.h>
#include <tencentcloud/core/NetworkProxy.h>
#include <tencentcloud/core/AsyncCallerContext.h>
#include <tencentcloud/sms/v20210111/SmsClient.h>
#include <tencentcloud/sms/v20210111/model/SendStatusStatisticsRequest.h>
#include <tencentcloud/sms/v20210111/model/SendStatusStatisticsResponse.h>

#include <cstdlib>
#include <iostream>
#include <string>

using namespace TencentCloud;
using namespace TencentCloud::Sms::V20210111;
using namespace TencentCloud::Sms::V20210111::Model;
using namespace std;

int main()
{
TencentCloud::InitAPI();

// use the sdk
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey, 切勿将密钥泄露给他人
// 硬编码密钥到代码中有可能随代码泄露而暴露,有安全隐患,并不推荐。
// 为了保护密钥安全,建议将密钥设置在环境变量中或者配置文件中。
// 前往 https://console.tencentcloud.com/cam/capi 获取 API 密钥 SecretId SecretKey
Credential cred = Credential(getenv("TENCENTCLOUD_SECRET_ID"),
getenv("TENCENTCLOUD_SECRET_KEY"));

// 实例化一个http选项,可选的,没有特殊需求可以跳过。
HttpProfile httpProfile = HttpProfile();
httpProfile.SetKeepAlive(true); // 状态保持,默认是False
httpProfile.SetEndpoint("sms.tencentcloudapi.com"); // 指定接入地域域名(默认就近接入)
httpProfile.SetReqTimeout(30); // 请求超时时间,单位为秒(默认60秒)
httpProfile.SetConnectTimeout(30); // 响应超时时间,单位是秒(默认是60秒)

ClientProfile clientProfile = ClientProfile(httpProfile);

SendStatusStatisticsRequest req = SendStatusStatisticsRequest();

/* 帮助链接:
* 短信控制台: https://console.tencentcloud.com/smsv2
*/
/* 短信应用 ID: 在 [短信控制台] 添加应用后生成的实际 SdkAppId,例如2400006666 */
req.SetSmsSdkAppId("2400006666");
// 最大上限,目前固定设置为0
req.SetLimit(0);
/* 偏移量,目前固定设置为0 */
req.SetOffset(0);
/* 设置起始时间,格式为yyyymmddhh,精确到小时,例如2021050113,表示2021年5月1号13时。 */
req.SetBeginTime("2019071100");
/* 设置结束时间,格式为yyyymmddhh,精确到小时,例如2021050118,表示2021年5月1号18时。
注:EndTime 必须大于 BeginTime。 */
req.SetEndTime("2019071123");

SmsClient sms_client = SmsClient(cred, "ap-singapore", clientProfile);

// set proxy
// NetworkProxy proxy = NetworkProxy(NetworkProxy::Type::HTTP, "localhost.proxy.com", 8080);
// cvm_client.SetNetworkProxy(proxy);

auto outcome = sms_client.SendStatusStatistics(req);
if (!outcome.IsSuccess())
{
cout << outcome.GetError().PrintAll() << endl;
TencentCloud::ShutdownAPI();
return -1;
}
SendStatusStatisticsResponse rsp = outcome.GetResult();
cout<<"RequestId="<<rsp.GetRequestId()<<endl;
cout<<"SendStatusStatisticsResponse="<<rsp.ToJsonString()<<endl;

TencentCloud::ShutdownAPI();

return 0;
}

申请短信模板

#include <tencentcloud/core/TencentCloud.h>
#include <tencentcloud/core/profile/HttpProfile.h>
#include <tencentcloud/core/profile/ClientProfile.h>
#include <tencentcloud/core/Credential.h>
#include <tencentcloud/core/NetworkProxy.h>
#include <tencentcloud/core/AsyncCallerContext.h>
#include <tencentcloud/sms/v20210111/SmsClient.h>
#include <tencentcloud/sms/v20210111/model/AddSmsTemplateRequest.h>
#include <tencentcloud/sms/v20210111/model/AddSmsTemplateResponse.h>

#include <cstdlib>
#include <iostream>
#include <string>

using namespace TencentCloud;
using namespace TencentCloud::Sms::V20210111;
using namespace TencentCloud::Sms::V20210111::Model;
using namespace std;

int main()
{
TencentCloud::InitAPI();

// use the sdk
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey, 切勿将密钥泄露给他人
// 硬编码密钥到代码中有可能随代码泄露而暴露,有安全隐患,并不推荐。
// 为了保护密钥安全,建议将密钥设置在环境变量中或者配置文件中。
// 前往 https://console.tencentcloud.com/cam/capi 获取 API 密钥 SecretId SecretKey
Credential cred = Credential(getenv("TENCENTCLOUD_SECRET_ID"),
getenv("TENCENTCLOUD_SECRET_KEY"));

// 实例化一个http选项,可选的,没有特殊需求可以跳过。
HttpProfile httpProfile = HttpProfile();
httpProfile.SetKeepAlive(true); // 状态保持,默认是False
httpProfile.SetEndpoint("sms.tencentcloudapi.com"); // 指定接入地域域名(默认就近接入)
httpProfile.SetReqTimeout(30); // 请求超时时间,单位为秒(默认60秒)
httpProfile.SetConnectTimeout(30); // 响应超时时间,单位是秒(默认是60秒)

ClientProfile clientProfile = ClientProfile(httpProfile);

AddSmsTemplateRequest req = AddSmsTemplateRequest();

/* 帮助链接:
* 短信控制台: https://console.tencentcloud.com/smsv2
*/
/* 模板名称 */
req.SetTemplateName("腾讯云");
/* 模板内容 */
req.SetTemplateContent("{1}为您的登录验证码,请于{2}分钟内填写,如非本人操作,请忽略本短信。");
/* 短信类型,1表示营销短信,2表示通知短信,3表示验证码短信 */
req.SetSmsType(3);
/* 是否国际/港澳台短信:
* 0:表示中国大陆地区短信
* 1:表示国际/港澳台短信 */
req.SetInternational(0);
/* 模板备注:例如申请原因,使用场景等 */
req.SetRemark("xxx");

SmsClient sms_client = SmsClient(cred, "ap-singapore", clientProfile);

// set proxy
// NetworkProxy proxy = NetworkProxy(NetworkProxy::Type::HTTP, "localhost.proxy.com", 8080);
// cvm_client.SetNetworkProxy(proxy);

auto outcome = sms_client.AddSmsTemplate(req);
if (!outcome.IsSuccess())
{
cout << outcome.GetError().PrintAll() << endl;
TencentCloud::ShutdownAPI();
return -1;
}
AddSmsTemplateResponse rsp = outcome.GetResult();
cout<<"RequestId="<<rsp.GetRequestId()<<endl;
cout<<"AddSmsTemplateResponse="<<rsp.ToJsonString()<<endl;

TencentCloud::ShutdownAPI();

return 0;
}


帮助和支持

本页内容是否解决了您的问题?

填写满意度调查问卷,共创更好文档体验。

文档反馈