本文为您介绍了C#调用SendMessageWithTemplate发送中国地区短信。

请求参数

名称 类型 是否必选 示例值 描述
To String 45***0121 接收方的手机号码;说明:号码格式为国家码+号码。
From String XXCompany 发送方标识;说明:发往中国时,该参数为短信签名。
TemplateCode String SMS_0000 短信模板ID。请在控制台中查看。
TemplateParam String {“code”:”1234”,”product”:”ytx”} 短信模板变量对应的实际值,JSON格式;说明:如果JSON中需要带换行符,请参照标准的JSON协议处理。
SmsUpExtendCode String 90999 上行短信扩展码,无特殊需要此字段的用户请忽略此字段。

返回数据

名称 类型 示例值 描述
To String 452220121 接收方的手机号码;说明:号码格式为国家码+号码。
MessageId String 10080303003003 短信发送ID,可根据该ID在接口QueryMessage中查询具体的发送状态。
ResponseCode String OK 请求状态码,返回OK代表请求成功,其他错误码详见错误码列表。
ResponseDescription String 短信接收成功 状态码的描述。
Segments String 1 短信计费条数。

引入阿里云核心包

调用示例

注意事项如下:

  • 初始化IClientProfile时,第一个参数(RegionId)必须为ap-southeast-1,请勿修改。
  • Domain必须为dysmsapi.ap-southeast-1.aliyuncs.com,请勿修改。
  • Version必须为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);
            }
        }
    }
}