コード例 #1
0
        /// <summary>
        /// 阿里云接口发送SMS信息
        /// </summary>
        /// <param name="PhoneNumbers">待发送手机号</param>
        /// <param name="TemplateCode">短信模板</param>
        /// <param name="data">短信模板</param>
        /// <param name="OutId">短信回执消息Id</param>
        private async Task <(bool success, string response)> AliYunSendSms(string PhoneNumbers, string TemplateCode, IDictionary <string, string> data, string OutId)
        {
            var sms = new SmsObject
            {
                Mobile     = PhoneNumbers,
                Signature  = SignName,
                TempletKey = TemplateCode,
                Data       = data,
                OutId      = OutId
            };

            var res = await new SendSMSByAliyunHandler(accessKeyId, accessSecret).Send(sms);

            return(res.success, res.response);

            //Debug.WriteLine($"发送结果:{res.success}");
            //Debug.WriteLine($"Response:{res.response}");


            //String product = "Dysmsapi";                                //短信API产品名称(短信产品名固定,无需修改)
            //String domain = "dysmsapi.aliyuncs.com";                    //短信API产品域名(接口地址固定,无需修改)
            //String version = "V1.0";                                    //版本

            //DefaultProfile profile = DefaultProfile.GetProfile("cn-hangzhou", accessKeyId, accessSecret);
            //IAcsClient client = new DefaultAcsClient(profile);
            //CommonRequest request = new CommonRequest(product, "V1.0", "SendSms");

            //request.Protocol = ProtocolType.HTTPS;
            //request.Method = MethodType.POST;
            //request.QueryParameters.Add("RegionId", "cn-hangzhou");
            //request.QueryParameters.Add("PhoneNumbers", PhoneNumbers);
            //request.QueryParameters.Add("SignName", SignName);
            //request.QueryParameters.Add("TemplateCode", TemplateCode);
            //request.QueryParameters.Add("TemplateParam", TemplateParam);
            //request.QueryParameters.Add("OutId", OutId);
            //try
            //{
            //    CommonResponse response = client.GetAcsResponse(request);
            //    string responseData = response.Data;

            //    return true;
            //}
        }
コード例 #2
0
        /// <summary>
        /// 发送短信
        /// </summary>
        public async Task <(bool success, string response)> Send(SmsObject sms)
        {
            var paramers = new Dictionary <string, string>();

            paramers.Add("PhoneNumbers", sms.Mobile);
            paramers.Add("SignName", sms.Signature);
            paramers.Add("TemplateCode", sms.TempletKey);
            paramers.Add("TemplateParam", JsonConvert.SerializeObject(sms.Data));
            paramers.Add("OutId", sms.OutId);
            paramers.Add("AccessKeyId", AccessKeyId);

            try
            {
                string url = GetSignUrl(paramers, AccessKeySecret);

                int retryTimes = 1;
                var reply      = await HttpGetAsync(url);

                while (500 <= reply.StatusCode && AutoRetry && retryTimes < MaxRetryNumber)
                {
                    url   = GetSignUrl(paramers, AccessKeySecret);
                    reply = await HttpGetAsync(url);

                    retryTimes++;
                }

                if (!string.IsNullOrEmpty(reply.response))
                {
                    var res = JsonConvert.DeserializeObject <Dictionary <string, string> >(reply.response);
                    if (res != null && res.ContainsKey("Code") && "OK".Equals(res["Code"]))
                    {
                        return(true, response : reply.response);
                    }
                }

                return(false, response : reply.response);
            }
            catch (Exception ex)
            {
                return(false, response : ex.Message);
            }
        }