【雲短信】騰訊&阿里

騰訊 : https://github.com/qcloudsms/qcloudsms_csharphtml

安裝nuget包:git

 

using qcloudsms_csharp;
using System.Collections.Generic;

namespace Own.Common
{
    public class Utils
    {
        /// <summary>
        /// 騰訊雲發短信
        /// </summary>
        public static void TencentSmsMethod()
        {
            //地址:https://console.cloud.tencent.com/sms/smsContent
            // 短信應用SDK AppID
            const int appid = 111;
            // 短信應用SDK AppKey
            const string appkey = "111";
            //模板Id
            int templateId = 269006;
            // 簽名 NOTE: 這裏的簽名只是示例,請使用真實的已申請的簽名, 簽名參數使用的是`簽名內容`,而不是`簽名ID`
            string smsSign = "程序源於生活";
            // 須要發送短信的手機號碼
            var phoneNumbers = new List<string> { "12", "12" };

            #region 指定模板單發短信   
            //var ssender = new SmsSingleSender(appid, appkey);
            //var result = ssender.sendWithParam("86", phoneNumbers[0],
            //    templateId, new[] { "5678","2" }, smsSign, "", "");  // 簽名參數未提供或者爲空時,會使用默認簽名發送短信

            #endregion

            #region 指定模板羣發
            var ssender = new SmsMultiSender(appid, appkey);
            var sresult = ssender.sendWithParam("86", phoneNumbers, templateId,
                new List<string> { "5678", "2" }, null, "", "");  // 簽名參數未提供或者爲空時,會使用默認簽名發送短信

            #endregion
        }
    }
}

 

 

阿里官方說明文檔
github

1.在github下載sdk api

https://github.com/aliyun/aliyun-openapi-net-sdkapp

在線調試:https://api.aliyun.com/new?spm=a2c4g.11186623.2.13.5c1619d9AB95cl#/?product=Dysmsapi&api=SendSmsdom

2.下載sdk和demo阿里雲

 

 

 

using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Dysmsapi.Model.V20170525;
using System;

namespace Own.Common
{
    public class Utils
    {
        /// <summary>
        /// 阿里雲發短信
        /// </summary>
        public static SendSmsResponse SendSms()
        {
            //產品名稱:雲通訊短信API產品,開發者無需替換
            const String product = "Dysmsapi";
            //產品域名,開發者無需替換
            const String domain = "dysmsapi.aliyuncs.com";

            // TODO 此處須要替換成開發者本身的AK(在阿里雲訪問控制檯尋找)
            const String accessKeyId = "yourAccessKeyId";
            const String accessKeySecret = "yourAccessKeySecret";

            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 = "15000000000";
                //必填:短信簽名-可在短信控制檯中找到
                request.SignName = "雲通訊";
                //必填:短信模板-可在短信控制檯中找到
                request.TemplateCode = "SMS_1000000";
                //可選:模板中的變量替換JSON串,如模板內容爲"親愛的${name},您的驗證碼爲${code}"時,此處的值爲
                request.TemplateParam = "{\"customer\":\"123\"}";
                //可選:outId爲提供給業務方擴展字段,最終在短信回執消息中將此值帶回給調用者
                request.OutId = "yourOutId";
                //請求失敗這裏會拋ClientException異常
                response = acsClient.GetAcsResponse(request);
            }
            catch (ServerException e)
            {
                Console.WriteLine(e.ErrorCode);
            }
            catch (ClientException e)
            {
                Console.WriteLine(e.ErrorCode);
            }

            return response;
        }

        /// <summary>
        /// 批量
        /// </summary>
        /// <returns></returns>
        public static SendBatchSmsResponse BatchSendSms()
        {
            //產品名稱:雲通訊短信API產品,開發者無需替換
            const String product = "Dysmsapi";
            //產品域名,開發者無需替換
            const String domain = "dysmsapi.aliyuncs.com";

            // TODO 此處須要替換成開發者本身的AK(在阿里雲訪問控制檯尋找)
            const String accessKeyId = "yourAccessKeyId";
            const String accessKeySecret = "yourAccessKeySecret";

            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
            {

                //必填:待發送手機號。支持JSON格式的批量調用,批量上限爲100個手機號碼,批量調用相對於單條調用及時性稍有延遲,驗證碼類型的短信推薦使用單條調用的方式
                request.PhoneNumberJson = "[\"1500000000\",\"1500000001\"]";
                //必填:短信簽名-支持不一樣的號碼發送不一樣的短信簽名
                request.SignNameJson = "[\"雲通訊\",\"雲通訊\"]";
                //必填:短信模板-可在短信控制檯中找到
                request.TemplateCode = "SMS_1000000";
                //必填:模板中的變量替換JSON串,如模板內容爲"親愛的${name},您的驗證碼爲${code}"時,此處的值爲
                //友情提示:若是JSON中須要帶換行符,請參照標準的JSON協議對換行符的要求,好比短信內容中包含\r\n的狀況在JSON中須要表示成\\r\\n,不然會致使JSON在服務端解析失敗
                request.TemplateParamJson = "[{\"name\":\"Tom\", \"code\":\"123\"},{\"name\":\"Jack\", \"code\":\"456\"}]";
                //可選-上行短信擴展碼(擴展碼字段控制在7位或如下,無特殊需求用戶請忽略此字段)
                //request.SmsUpExtendCodeJson = "[\"90997\",\"90998\"]";

                //請求失敗這裏會拋ClientException異常
                response = acsClient.GetAcsResponse(request);

            }
            catch (ServerException e)
            {
                Console.Write(e.ErrorCode);
            }
            catch (ClientException e)
            {
                Console.Write(e.ErrorCode);
                Console.Write(e.Message);
            }

            return response;
        }

        /// <summary>
        /// 查詢發送詳情
        /// </summary>
        public static QuerySendDetailsResponse QuerySendDetails(String bizId)
        {
            //產品名稱:雲通訊短信API產品,開發者無需替換
            const String product = "Dysmsapi";
            //產品域名,開發者無需替換
            const String domain = "dysmsapi.aliyuncs.com";

            // TODO 此處須要替換成開發者本身的AK(在阿里雲訪問控制檯尋找)
            const String accessKeyId = "yourAccessKeyId";
            const String accessKeySecret = "yourAccessKeySecret";

            //初始化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 = "15000000000";
            //可選-流水號
            request.BizId = bizId;
            //必填-發送日期 支持30天內記錄查詢,格式yyyyMMdd       
            request.SendDate = DateTime.Now.ToString("yyyyMMdd");
            //必填-頁大小
            request.PageSize = 10;
            //必填-當前頁碼從1開始計數
            request.CurrentPage = 1;

            QuerySendDetailsResponse querySendDetailsResponse = null;
            try
            {
                querySendDetailsResponse = acsClient.GetAcsResponse(request);
            }
            catch (ServerException e)
            {
                Console.WriteLine(e.ErrorCode);
            }
            catch (ClientException e)
            {
                Console.WriteLine(e.ErrorCode);
            }
            return querySendDetailsResponse;
        }
    }
}
相關文章
相關標籤/搜索