c#阿里雲短信驗證碼

發送驗證碼json

private static void SendAcs(string mobile, string templateCode, dynamic json, int ourid)
        {
            if (string.IsNullOrEmpty(mobile))
            {
                throw new Exception("mobile不能爲空");
            }
            if (string.IsNullOrEmpty(templateCode))
            {
                throw new Exception("templateCode不能爲空");
            }
            string product = "Dysmsapi";//短信API產品名稱(短信產品名固定,無需修改)
            string domain = "dysmsapi.aliyuncs.com";//短信API產品域名(接口地址固定,無需修改)
            string accessKeyId = "xxxxxxxxxx";//你的accessKeyId,參考本文檔步驟2
            string accessKeySecret = "xxxxxxxxx";//你的accessKeySecret,參考本文檔步驟2

            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
            {
                //必填:待發送手機號。支持以逗號分隔的形式進行批量調用,批量上限爲1000個手機號碼,批量調用相對於單條調用及時性稍有延遲,驗證碼類型的短信推薦使用單條調用的方式,發送國際/港澳臺消息時,接收號碼格式爲00+國際區號+號碼,如「0085200000000」
                request.PhoneNumbers = mobile;
                //必填:短信簽名-可在短信控制檯中找到
                request.SignName = "xxxxxx";
                //必填:短信模板-可在短信控制檯中找到,發送國際/港澳臺消息時,請使用國際/港澳臺短信模版
                request.TemplateCode = templateCode;
                //可選:模板中的變量替換JSON串,如模板內容爲"親愛的${name},您的驗證碼爲${code}"時,此處的值爲
                request.TemplateParam = JsonConvert.SerializeObject(json);// "{\"name\":\"Tom\",\"code\":\"123\"}";
                //可選:outId爲提供給業務方擴展字段,最終在短信回執消息中將此值帶回給調用者
                request.OutId = ourid.ToString();// "yourOutId";
                //請求失敗這裏會拋ClientException異常
                SendSmsResponse sendSmsResponse = acsClient.GetAcsResponse(request);
                if (sendSmsResponse.BizId == null)
                    throw new ApplicationException(sendSmsResponse.Message);
                System.Console.WriteLine(sendSmsResponse.Message);
            }
            catch (ServerException ex)
            {
                throw new ApplicationException(ex.Message);
                //System.Console.WriteLine("Hello World!");
            }
            catch (ClientException ex)
            {
                throw new ApplicationException(ex.Message);
                //System.Console.WriteLine("Hello World!");
            }
        }
相關文章
相關標籤/搜索