tencent cloud

Short Message Service

Release Notes and Announcements
Announcements
Product Introduction
Glossary
Product Overview
Review Standards
Notes
Signature Review Standards
Body Template Review Standards
Sample Body Templates
Reasons for Rejection
Global SMS
Global SMS Intro
Global SMS Pricing
Getting Started
Console Guide
Console Overview
Global SMS
Marketing Management
Business Statistics
Application Management
General Management
Access Management
Practical Tutorial
How to Send Bulk Best Wishes Messages
How to Send SMS Verification Codes
API Documentation
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
SMS Callback APIs
SDK Documentation
SDK Download
SDK for Java
SDK for PHP
SDK for Python
SDK for Node.js
SDK for C#
SDK for Go
SDK for C++
FAQ
Billing
Security
Signature
Template
Message Content
Others
Service Agreement
Tencent Cloud SMS Data Processing and Security Agreement
Tencent Cloud SMS Privacy Policy
Service Level Agreement
Tencent Cloud SMS Services Terms of Service
Contact Us

SDK for C++

PDF
Focus Mode
Font Size
Last updated: 2024-06-27 15:48:00
SDK 3.0 is a companion tool for the TencentCloud API 3.0 platform. You can use all SMS APIs through the SDK. The new SDK version is unified and features the same SDK usage, API call methods, error codes, and returned packet formats for different programming languages.
Note:
API version required for connecting to Tencent Cloud International::
SMS API v2021-01-11 is required. For details, see the sample code.
SMS delivery APIs: :
One message can be sent to up to 200 numbers at a time.
Signature and body template APIs: :
Individual users have no permission to use signature and body template APIs and can only manage SMS signatures and SMS body templates in the SMS console. To use the APIs, change "Individual Verification" to "Organization Verification". For details, see Identity Verification Change Guide.

Prerequisites

You have learned about the concept of Region and selected a region as needed.
You have activated the SMS service and created a signature and SMS template that have been approved. For details, see Getting Started.
You have got the SecretId and SecretKey on the Manage API Key page in the CAM console.
SecretId is used to identify the API caller.
SecretKey is a key used to encrypt the signature and help the server verify the signature. You should keep it private and avoid disclosure.
The endpoint of the SMS service is sms.tencentcloudapi.com.

Reference

For more information on the APIs and their parameters, see API Documentation.
You can download the SDK source code from Github Repository.

Installing SDK

Environmental Dependencies

See Github repository: Environmental Dependencies

SDK build from source code

See Github repository: SDK build from source code

Sample Code

Note:
All samples are for reference only and cannot be directly compiled and executed. You need to modify them based on your actual needs. You can also use API 3.0 Explorer to automatically generate the demo code as needed.
Each API has its own request and response structures. This document only lists the sample code of several common features as shown below:

Sending SMS messages

#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
// To protect key security, it is suggested to set keys in environment variables or configuration files. // Hardcoding keys into the code might lead to exposure through leaked code, posing a security vulnerability, and is not recommended.
// To ensure key security, it's recommended to set the keys in environment variables or a configuration file.
// Go to https://console.tencentcloud.com/cam/capi to obtain the API key SecretId SecretKey
Credential cred = Credential(getenv("TENCENTCLOUD_SECRET_ID"),
getenv("TENCENTCLOUD_SECRET_KEY"));
// (Optional) Instantiate an HTTP option.
HttpProfile httpProfile = HttpProfile();
httpProfile.SetKeepAlive(true); // Specify whether to enable the keepalive feature. The default value is “false”.
httpProfile.SetEndpoint("sms.tencentcloudapi.com"); // Specify the endpoint. If you do not specify it, nearby access is enabled by default.
httpProfile.SetReqTimeout(10); // Specify the request timeout value, in seconds. The default value is 60s.
httpProfile.SetConnectTimeout(10); // Specify the response timeout value, in seconds. The default value is 60s.

ClientProfile clientProfile = ClientProfile(httpProfile);

SendSmsRequest req = SendSmsRequest();

/* Help link:
* SMS console: https://console.tencentcloud.com/smsv2
*/
/* SMS application ID: The SdkAppId generated after an application is added in the [SMS console], such as 2400006666 */
req.SetSmsSdkAppId("2400006666");

/* SMS signature content: You must enter an approved signature that is encoded in UTF-8.*/
req.SetSignName("Tencent");

/* Template ID: You must enter the ID of an approved template.*/
req.SetTemplateId("449739");

/* Template parameter: The number of template parameters must be the same as that of the variables in the template. If no template parameters are involved, leave this field empty.*/
req.SetTemplateParamSet(std::vector<std::string>{"1234"});

/* Target mobile number in the E.164 standard (+[country/region code][mobile number])
* Example: +60198890000, which has a “+” sign followed by 60 (country/region code) and then by 198890000 (mobile number). Up to 200 mobile numbers are supported */
req.SetPhoneNumberSet(std::vector<std::string>{"+60198890000"});

/* (Ignorable) User session content: Context information like the user ID can be carried and will be returned by the server as is.*/
req.SetSessionContext("");

/* (Ignorable) SMS code number extension, which is not activated by default. If you need to activate it, submit a ticket.*/
req.SetExtendCode("");

/* (Ignorable) SenderId for Global SMS, which is not activated by default. If you need to activate it, submit a ticket. For Chinese Mainland SMS, leave it empty.*/
req.SetSenderId("");

/* Instantiate the client object of the product (with SMS as an example) to be requested.
* The second parameter is used to specify region information. You can enter a string like "ap-singapore". For more supported regions, see https://www.tencentcloud.com/document/api/382/40466?lang=en#region-list */
SmsClient sms_client = SmsClient(cred, "ap-singapore", clientProfile);

// (Ignorable) Set a proxy.
// 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();

/* Below are some error codes and the corresponding solutions to them:
* [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;
}

Pulling receipt status

#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
// Instantiate an authentication object. Enter Tencent Cloud SecretId and SecretKey as input parameters, do not reveal the key to others
// To protect key security, it is suggested to set keys in environment variables or configuration files. // Hardcoding keys into the code might lead to exposure through leaked code, posing a security vulnerability, and is not recommended.
// Go to https://console.tencentcloud.com/cam/capi to obtain the API key SecretId SecretKey
Credential cred = Credential(getenv("TENCENTCLOUD_SECRET_ID"),
getenv("TENCENTCLOUD_SECRET_KEY"));

// (Optional) Instantiate an HTTP option.
HttpProfile httpProfile = HttpProfile();
httpProfile.SetKeepAlive(true); // Specify whether to enable the keepalive feature. The default value is “false”.
httpProfile.SetEndpoint("sms.tencentcloudapi.com"); // Specify the endpoint. If you do not specify it, nearby access is enabled by default.
httpProfile.SetReqTimeout(30); // Specify the request timeout value, in seconds. The default value is 60s.
httpProfile.SetConnectTimeout(30); // Specify the response timeout value, in seconds. The default value is 60s.

ClientProfile clientProfile = ClientProfile(httpProfile);

PullSmsSendStatusRequest req = PullSmsSendStatusRequest();

/* Help link:
* SMS console: https://console.tencentcloud.com/smsv2
*/
/* SMS application ID, which is the SdkAppId generated after an application is added in the [SMS console], such as 2400006666 */
req.SetSmsSdkAppId("2400006666");
// Set the max number of pulled entries. Max value: 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;
}

Collecting SMS sending statistics

#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
// Instantiate an authentication object. Enter Tencent Cloud SecretId and SecretKey as input parameters, do not reveal the key to others
// To protect key security, it is suggested to set keys in environment variables or configuration files. // Hardcoding keys into the code might lead to exposure through leaked code, posing a security vulnerability, and is not recommended.
// Go to https://console.tencentcloud.com/cam/capi to obtain the API key SecretId SecretKey
Credential cred = Credential(getenv("TENCENTCLOUD_SECRET_ID"),
getenv("TENCENTCLOUD_SECRET_KEY"));

// (Optional) Instantiate an HTTP option.
HttpProfile httpProfile = HttpProfile();
httpProfile.SetKeepAlive(true); // Specify whether to enable the keepalive feature. The default value is “false”.
httpProfile.SetEndpoint("sms.tencentcloudapi.com"); // Specify the endpoint. If you do not specify it, nearby access is enabled by default.
httpProfile.SetReqTimeout(30); // Specify the request timeout value, in seconds. The default value is 60s.
httpProfile.SetConnectTimeout(30); // Specify the response timeout value, in seconds. The default value is 60s.

ClientProfile clientProfile = ClientProfile(httpProfile);

SendStatusStatisticsRequest req = SendStatusStatisticsRequest();

/* Help link:
* SMS console: https://console.tencentcloud.com/smsv2
*/
/* SMS application ID, which is the SdkAppId generated after an application is added in the [SMS console], such as 2400006666 */
req.SetSmsSdkAppId("2400006666");
// Upper limit, which is currently fixed at 0.
req.SetLimit(0);
/* Offset, which is currently fixed at 0 */
req.SetOffset(0);
/* Start time in the format of “yyyymmddhh” accurate down to the hour, such as 2021050113 (13:00 on May 1, 2021).*/
req.SetBeginTime("2019071100");
/* End time in the format of “yyyymmddhh” accurate down to the hour, such as 2021050118 (18:00 on May 1, 2021).*/
* Note: “EndTime” must be after “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;
}

Applying for SMS template

#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
// Instantiate an authentication object. Enter Tencent Cloud SecretId and SecretKey as input parameters, do not reveal the key to others
// To protect key security, it is suggested to set keys in environment variables or configuration files. // Hardcoding keys into the code might lead to exposure through leaked code, posing a security vulnerability, and is not recommended.
// Go to https://console.tencentcloud.com/cam/capi to obtain the API key SecretId SecretKey
Credential cred = Credential(getenv("TENCENTCLOUD_SECRET_ID"),
getenv("TENCENTCLOUD_SECRET_KEY"));

// (Optional) Instantiate an HTTP option.
HttpProfile httpProfile = HttpProfile();
httpProfile.SetKeepAlive(true); // Specify whether to enable the keepalive feature. The default value is “false”.
httpProfile.SetEndpoint("sms.tencentcloudapi.com"); // Specify the endpoint. If you do not specify it, nearby access is enabled by default.
httpProfile.SetReqTimeout(30); // Specify the request timeout value, in seconds. The default value is 60s.
httpProfile.SetConnectTimeout(30); // Specify the response timeout value, in seconds. The default value is 60s.

ClientProfile clientProfile = ClientProfile(httpProfile);

AddSmsTemplateRequest req = AddSmsTemplateRequest();

/* Help link:
* SMS console: https://console.tencentcloud.com/smsv2
*/
/* Template name */
req.SetTemplateName("Tencent");
/* Template content */
req.SetTemplateContent("{Your login verification code is {1}. Please enter it within {2} minutes. If the login was not initiated by you, please ignore this message");
/* SMS type. 1: Marketing SMS, 2: Notification SMS, 3: OTP SMS */
req.SetSmsType(3);
/* A parameter used to specify whether it is Global SMS:
* 0: Chinese Mainland SMS
* 1: Global SMS.*/
req.SetInternational(0);
/* Template remarks, such as the reason for application and its use cases.*/
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;
}


Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback