.Net Core 阿里云短信服务Demo

话不多说直接Po 代码  官方demo及sdk 下载地址 :http://ytx-sdk.oss-cn-shanghai.aliyuncs.com/dysms_net.zip?spm=a2c4g.11186623.2.19.497f4175i94E9Y&file=dysms_net.zip
我是下的demo。自己试了下。小改了没啥问题直接用了。几乎和demo一样。.就是sdk引用踩了点坑.看好了引用

.Net Core 阿里云短信服务Demo_第1张图片

唯一踩坑的点就是包要引用对。不然下面这部会报错。没实例化。
引用的。

以及我引用的是core2.0下面的sdk 。这2个不引用会报错。不通的包里有相同的类型导致编译不过去。
.Net Core 阿里云短信服务Demo_第2张图片

参数


 private readonly string commonTemplate = "xxxx";       //通用短信模板id
 private readonly string messageSignature = "xxx";      //短信标识 title
 private readonly string product = "Dysmsapi";               //云通信短信API产品名 (固定的)
 private readonly string domain = "dysmsapi.aliyuncs.com";   //域名 (固定的)
 private readonly string accessKeyId = "xxx";
 private readonly string accessKeySecret = "xxxx";

单个短信发送代码 :

        /// 
        /// 单个发送短息
        /// 
        /// 
        /// 
        /// 
        public (bool result, string msg, string code) sendSms(string phone, string vcode, int type)
        {
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", accessKeyId, accessKeySecret);
            DefaultProfile.AddEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);
            SendSmsRequest request = new SendSmsRequest();
            SendSmsResponse response = null;
            try
            {
                //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
                request.PhoneNumbers = phone;           //必填:待发送手机号。可以,间隔发送多个
                request.SignName = messageSignature;    //必填:短信签名
                request.TemplateCode = type == 1 ? registerTemplate : type == 2 ? loginTemplate : commonTemplate;        //必填:短信模板
                request.TemplateParam = new { code = vcode }.ToJson();    //模板的替换变量。根据模板。可以替换对应的字段。
                //request.OutId = "yourOutId";          //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者

                //请求失败这里会抛ClientException异常
                response = acsClient.GetAcsResponse(request);

                if (response.Code.Equals("OK"))
                    return (true, string.Empty, response.Code);
                else
                    return (false, response.Message, response.Code);
            }
            catch (ServerException e)
            {
                //Console.WriteLine(e.ErrorCode);
                return (false, e.ErrorCode, response.Code);
            }
            catch (ClientException e)
            {
                //Console.WriteLine(e.ErrorCode);
                return (false, e.ErrorCode, response.Code);
            }
        }

给多个手机号发不同的短信  :

 public SendBatchSmsResponse sendAnySms(List codeList, List phoneList)
        {
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", accessKeyId, accessKeySecret);
            DefaultProfile.AddEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);

            IAcsClient acsClient = new DefaultAcsClient(profile);
            SendBatchSmsRequest request = new SendBatchSmsRequest();
            //request.Protocol = ProtocolType.HTTPS;
            //request.TimeoutInMilliSeconds = 1;

            SendBatchSmsResponse response = null;
            try
            {
                if (codeList.Count != phoneList.Count) return null;

                var vcode = "12345";
                var vcode2 = "54321";


                //必填:待发送手机号。支持JSON格式的批量调用,批量上限为100个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
                request.PhoneNumberJson = "[\"112345698\",\"112345698\"]";
                //必填:短信签名-支持不同的号码发送不同的短信签名
                request.SignNameJson = "[\"生物\",\"生物\"]";
                //必填:短信模板-可在短信控制台中找到
                request.TemplateCode = commonTemplate;
                //必填:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
                //友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败
                request.TemplateParamJson = "[{\"code\":\"" + vcode + "\"},{\"code\":\"" + vcode2 + "\"}]";
                //可选-上行短信扩展码(扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段)
                //request.SmsUpExtendCodeJson = "[\"90997\",\"90998\"]";

                //请求失败这里会抛ClientException异常
                response = acsClient.GetAcsResponse(request);

            }
            catch (ServerException e)
            {
                //Console.Write(e.ErrorCode);
                return null;
            }
            catch (ClientException e)
            {
                //Console.Write(e.ErrorCode);
                //Console.Write(e.Message);
                return null;
            }
            return response;

        }

查询某一天的发送短信情况

  public (bool result, string msg, List info) querySendDetails(string phone, string date, int pageIndex = 1, int pageSize = 10, String bizId = "")
        {
            //初始化acsClient,暂不支持region化
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", accessKeyId, accessKeySecret);
            DefaultProfile.AddEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);
            //组装请求对象
            QuerySendDetailsRequest request = new QuerySendDetailsRequest();
            //必填-号码
            request.PhoneNumber = phone;
            可选-流水号
            //request.BizId = bizId;

            //必填-发送日期 支持30天内记录查询,格式yyyyMMdd       
            request.SendDate = DateTime.Now.ToString(date);
            //必填-页大小
            request.PageSize = pageSize;
            //必填-当前页码从1开始计数
            request.CurrentPage = pageIndex;

            QuerySendDetailsResponse querySendDetailsResponse = null;
            try
            {
                querySendDetailsResponse = acsClient.GetAcsResponse(request);

                if (querySendDetailsResponse.Code.Equals("OK"))
                    return (true, string.Empty, querySendDetailsResponse.SmsSendDetailDTOs);
                else
                    return (false, querySendDetailsResponse.Message, null);
            }
            catch (ServerException e)
            {
                //Console.WriteLine(e.ErrorCode);
                return (false, e.ErrorCode, null);
            }
            catch (ClientException e)
            {
                //Console.WriteLine(e.ErrorCode);
                return (false, e.ErrorCode, null);
            }
        }

 

你可能感兴趣的:(.Net,Core,学习总结)