This topic describes how to use the Alibaba Cloud SDK for C# to call the SendMessageWithTemplate operation. This API operation allows you to send text messages to Mainland China.
Request parameters
Parameter | Type | Required | Example | Description |
---|---|---|---|---|
To | String | Yes | 45***0121 | The mobile phone number to receive the text message. Note that you must add the country code to the beginning of the mobile phone number. |
From | String | No | XXCompany | The ID of the sender. This parameter specifies the text message signature when you send the text message to Mainland China. |
TemplateCode | String | Yes | SMS_0000 | The ID of the text message template. You can view the ID of the text message template in the console of Short Message Service (SMS). |
TemplateParam | String | No | {"code":"1234","product":"ytx"} | The values of the variables that are included in the text message template. The values must use the JSON format. If line feeds are required in JSON-formatted data, the line feeds must meet the relevant requirements that are specified in the standard JSON protocol. |
SmsUpExtendCode | String | No | 90999 | The extension code of the upstream text message. This parameter is optional. You can skip this parameter based on your business requirements. |
Response parameters
Parameter | Type | Example | Description |
---|---|---|---|
To | String | 452220121 | The mobile phone number that received the text message. Note that the country code is added to the beginning of the mobile phone number. |
MessageId | String | 10080303003003 | The ID of the text message. You can call the QueryMessage operation to query the delivery status of the text message based on the ID of the text message. |
ResponseCode | String | OK | The status code of the request. If the API operation returns OK, the request is successful. For more information about the other error codes, see Error codes. |
ResponseDescription | String | The text message is received. | The description of the status code. |
Segments | String | 1 | The number of the text messages that incurred fees. |
Install the core library of the Alibaba Cloud SDK for C#
- Method 1: Use the NuGet package manager. For more information, see Installation guide.
- Method 2: Download the aliyun-net-sdk-core library by using the following link: Core library of the Alibaba Cloud SDK for C#. The SDK allows you to call SMS API operations.
Sample requests
The considerations are described as follows:
- When you initialize the IClientProfile class, you must use the default value of the RegionId parameter. The default value is ap-southeast-1. Note that RegionId is the first parameter in the IClientProfile class.
- You must use the default value of the Domain parameter: dysmsapi.ap-southeast-1.aliyuncs.com.
- You must use the default value of the Version parameter: 2018-05-01.
using System;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Http;
namespace CommonRpc
{
class Program
{
static void Main(string[] args)
{
IClientProfile profile = DefaultProfile.GetProfile("ap-southeast-1", "<AccessKeyId>", "<AccessSecret>");
DefaultAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest
{
Method = MethodType.POST,
Domain = "dysmsapi.ap-southeast-1.aliyuncs.com",
Version = "2018-05-01",
Action = "SendMessageWithTemplate"
};
// request.Protocol = ProtocolType.HTTPS;
request.AddQueryParameters("To", "8615200000000");
request.AddQueryParameters("From", "SMS signature");
request.AddQueryParameters("TemplateCode", "SMS_225001");
request.AddQueryParameters("TemplateParam", "{\"code\":\"1234\"}");
request.AddQueryParameters("SmsUpExtendCode", "12345");
try
{
CommonResponse response = client.GetCommonResponse(request);
Console.WriteLine(response.Data);
}
catch (ServerException e)
{
Console.WriteLine(e);
}
catch (ClientException e)
{
Console.WriteLine(e);
}
}
}
}