本文为您介绍短信服务.NET SDK的使用方法及示例。
前提条件
安装.NET SDK核心库
- 使用依赖包工具安装(推荐)
您可以通过NuGet程序包管理器来安装,在解决方案资源管理器面板中,右键单击您的项目选择管理NuGet程序包菜单,在打开的NuGet管理面板中单击浏览选项卡输入aliyun-net-sdk-core,选择并单击安装即可。
- 通过.NET CLI工具来安装核心库
dotnet add package aliyun-net-sdk-core
- 自行下载安装
您可以使用
git clone
或其它手段下载aliyun-net-sdk-core并自行添加解决方案。下载安装包,请参见SDK 核心库。
发送短信
- 代码示例
using System; using Aliyun.Acs.Core; using Aliyun.Acs.Core.Profile; using Aliyun.Acs.Core.Exceptions; using Aliyun.Acs.Core.Http; namespace CommonRequestDemo { class Program { static void Main(string[] args) { IClientProfile profile = DefaultProfile.GetProfile("ap-northeast-1", "<accessKeyId>", "<accessSecret>"); DefaultAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); request.Method = MethodType.POST; request.Domain = "dysmsapi.aliyuncs.com"; request.Version = "2017-05-25"; request.Action = "SendSms"; // request.Protocol = ProtocolType.HTTP; request.AddQueryParameters("PhoneNumbers", "1503871****"); //接收短信的手机号码 request.AddQueryParameters("SignName", "阿里云测试专用"); //短信签名名称 request.AddQueryParameters("TemplateCode", "SMS_20933****"); //短信模板CODE request.AddQueryParameters("TemplateParam", "{\"code\":\"1111\"}"); //短信模板变量对应的实际值,JSON格式 try { CommonResponse response = client.GetCommonResponse(request); Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content)); } catch (ServerException e) { Console.WriteLine(e); } catch (ClientException e) { Console.WriteLine(e); } } } }
- 结果示例
{ "RequestId": "614048FB-0619-4439-A1D5-AA8B218A****", "Message": "OK", "BizId": "386715418801811068^0", "Code": "OK"}
相关文档
- 如果您想了解更多示例,请参见OpenAPI开发者门户。
说明 登录开发者门户平台,SDK版本选择时,请选择原版 SDK。
- 短信服务相关API,请参见API概览。