.net core 腾讯短信发送

①、安装TencentCloudSDK

②、代码:

       

        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="sms"></param>
        /// <returns></returns>
        public async Task<AjaxResult> SingleSendAsync(SendSms sms)
        {
            Credential credential = new Credential()
            {
                SecretId = _section.GetValue<string>("secretId"),
                SecretKey = _section.GetValue<string>("secretKey")
            };
            if (sms.Phone == null || sms.Phone.Length < 1)
                return new AjaxResult(StatusCodes.Status400BadRequest) {message = "请填写接收人手机号码"};
            for (int i = 0; i < sms.Phone.Length; )
            {
                if (string.IsNullOrEmpty(sms.Phone[i]))
                    continue;
                sms.Phone[i] = $"+86{sms.Phone[i]}";
                i++;
            }
            //ClientProfile profile = new ClientProfile();
            //profile.SignMethod = ClientProfile.SIGN_TC3SHA256;
            SmsClient client = new SmsClient(credential, "ap-shenzhen");
            SendSmsRequest request = new SendSmsRequest
            {
                Sign= "签名名称",
                PhoneNumberSet = sms.Phone,//new[] { "+8613476285560" },
                TemplateID = sms.tempId,
                TemplateParamSet = sms.tempParam,  //new[] { "12" }
                SmsSdkAppid= _section.GetValue<string>("appId")
            };

            SendSmsResponse response = await client.SendSms(request);
            return new AjaxResult(Microsoft.AspNetCore.Http.StatusCodes.Status200OK);
            //response.SendStatusSet
            //return new AjaxResult(result.result == 0 ? HttpStatusCode.OK : HttpStatusCode.InternalServerError, new { result.errMsg, result.ext, result.fee, result.sid });
        }

猜你喜欢

转载自www.cnblogs.com/study10000/p/12895509.html