合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
## 文件路径 ``` aliyunSMS/aliyunsms.cs ``` ## 代码 ``` using System; using System.Collections.Generic; using System.Linq; using System.Text; using Aliyun.Acs.Core; using Aliyun.Acs.Core.Exceptions; using Aliyun.Acs.Core.Profile; using Aliyun.Acs.Dysmsapi.Model.V20170525; namespace NetWing.Common { public class aliyunsms { public string send(string recnum, string smstemplatecode, string smsparam) { String product = "Dysmsapi";//短信API产品名称(短信产品名固定,无需修改) String domain = "dysmsapi.aliyuncs.com";//短信API产品域名(接口地址固定,无需修改) String accessKeyId = NetWing.Common.ConfigHelper.GetValue("accessKeyId");//你的accessKeyId,参考本文档步骤2 String accessKeySecret = NetWing.Common.ConfigHelper.GetValue("aliaccessKeySecret");//你的accessKeySecret,参考本文档步骤2 String SignName = NetWing.Common.ConfigHelper.GetValue("SignName"); //短信签名 IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", accessKeyId, accessKeySecret); //IAcsClient client = new DefaultAcsClient(profile); // SingleSendSmsRequest request = new SingleSendSmsRequest(); //初始化ascClient,暂时不支持多region(请勿修改) DefaultProfile.AddEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); IAcsClient acsClient = new DefaultAcsClient(profile); SendSmsRequest request = new SendSmsRequest(); try { //request.AcceptFormat = Aliyun.Acs.Core.Http.FormatType.JSON; //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式 request.PhoneNumbers = recnum; //必填:短信签名-可在短信控制台中找到 request.SignName =SignName; //必填:短信模板-可在短信控制台中找到 request.TemplateCode = smstemplatecode; //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 request.TemplateParam = smsparam; //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 request.OutId = "ok"; //请求失败这里会抛ClientException异常 SendSmsResponse sendSmsResponse = acsClient.GetAcsResponse(request); //System.Console.WriteLine(sendSmsResponse.Message); return sendSmsResponse.Message; } catch (ServerException e) { //System.Console.WriteLine("Hello World!"); return e.ErrorMessage; } catch (ClientException e) { //System.Console.WriteLine("Hello World!"); return e.ErrorMessage; } } } } ``` ## 调用方法 ``` RandomHelper r = new RandomHelper(); string tels = "13700225160"; string templateno = "SMS_51209300"; string smsparam = "{\"code\":\""+r.GetRandomInt(1000,9999)+"\",\"product\":\"云南便民\"}"; //验证码${ code},您正在尝试变更${ product}重要信息,请妥善保管账户信息。 aliyunsms alisms = new aliyunsms(); string json = alisms.send(tels, templateno, smsparam); Response.Write(json); ``` ## 注意:阿里信息保存在web.config里(一下敏感信息已处理) ``` <appSettings> <add key="accessKeyId" value="LTAI221qVC3UPyCJ2aKB" /> <!--阿里短信accessKeyId--> <add key="aliaccessKeySecret" value="S212HT9bS9x2U0eVw7CFmHiXOD08rm1ZKf" /> <!--阿里短信accesskeysecert--> <add key="SignName" value="翼通" /> <!--阿里短信阿里短信签名--> </appSettings> ```