移動MAS短信平台發送短信


MAS短信平台發送短信分為兩種方式

參考文檔下載

一、sdk調用

 

using mas.ecloud.sdkclient;
using System;

namespace 短信發送
{
    class Program
    {
        private static Client client = Client.instance;
        static void Main(string[] args)
        {
            string url = "";//身份認證地址,向客戶經理獲取。
            string apId = "";//賬號
            string secretKey = "";//密碼
            string ecName = "";    //集團名稱
            string sign = "";    //網關簽名編碼
            string mobiles = "";//電話號碼
            string content = "測試短信發送";//短信內容
            bool loginResult = client.login(url, apId, secretKey, ecName);
            Console.WriteLine("登錄結果:" + loginResult);
                if (loginResult)
                {
                    // 發送短信 
                    int sendResult = client.sendDSMS(new string[] { mobiles }, content, "", 5, sign, Guid.NewGuid().ToString());
                     Console.WriteLine("發送結果:" + sendResult);               

            }
}
    }
}

所需sdk

二、http調用

 

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace 短信發送
{
    public class SMSUtils
    {
        private static String apId = "";//用戶名
        private static String secretKey = "";//密碼
        private static String ecName = "";    //集團名稱
        private static String sign = "";    //網關簽名編碼
        private static String addSerial = "";   //拓展碼 填空
        public static String url = "http://112.35.1.155:1992/sms/norsubmit";//請求urlhttp://112.35.1.155:1992/sms/norsubmit
        private static String mobiles = "";//電話號碼(多個電話號碼用逗號隔開)
        private static String content = "測試短信發送";//短信內容
        public SMSUtils()
        {

        }
        public void  SendMessage()
        {                 
            SendReq sendReq = new SendReq();             
            sendReq.apId = apId;
            sendReq.ecName = ecName;
            sendReq.secretKey = secretKey;
            sendReq.content = content;
            sendReq.mobiles = mobiles;
            sendReq.addSerial = addSerial;
            sendReq.sign = sign;
            sendReq.mac = UserMd5(sendReq.ecName + sendReq.apId + sendReq.secretKey + sendReq.mobiles + sendReq.content + sendReq.sign);
            var reqText = JsonConvert.SerializeObject(sendReq);
            string encode = Convert.ToBase64String(Encoding.UTF8.GetBytes(reqText));
            sendPost(url, encode);

        }
        #region MyRegion
        private static void sendPost(string url, string param)
        {

            //創建HttpClient(注意傳入HttpClientHandler)
            var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
            using (var http = new HttpClient(handler))
            {
                var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(param), Encoding.UTF8, "application/json");
                var response = http.PostAsync(url, content).Result;//傳參使用                                                                  
                response.EnsureSuccessStatusCode(); //確保HTTP成功狀態值R
                //await異步讀取最后的JSON(注意此時gzip已經被自動解壓縮了,因為上面的AutomaticDecompression = DecompressionMethods.GZip)
                var result = response.Content.ReadAsStringAsync().Result;
                var json = JsonConvert.DeserializeObject<SendRes>(result);
                bool success = json.success;
            }
        }

        #endregion
        //MD5加密
        public string UserMd5(string str)
        {
            string cl = str;
            string pwd = "";
            MD5 md5 = MD5.Create();//實例化一個md5對像
            // 加密后是一個字節類型的數組,這里要注意編碼UTF8/Unicode等的選擇 
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
            // 通過使用循環,將字節類型的數組轉換為字符串,此字符串是常規字符格式化所得
            for (int i = 0; i < s.Length; i++)
            {
                // 將得到的字符串使用十六進制類型格式。格式后的字符是小寫的字母,如果使用大寫(X)則格式后的字符是大寫字符
                pwd = pwd + s[i].ToString("x2");
            }
            return pwd;
        }

    }



    /**
     * 發送短信請求實體
     */
    public class SendReq
    {
        public string ecName { get; set; }     //集團客戶名稱
        public string apId { get; set; }        //用戶名
        public string secretKey { get; set; }           //密碼
        public string mobiles { get; set; }     //手機號碼逗號分隔。(如“18137282928,18137282922,18137282923”)
        public string content { get; set; }     //發送短信內容
        public string sign { get; set; }        //網關簽名編碼,必填,簽名編碼在中國移動集團開通帳號后分配,可以在雲MAS網頁端管理子系統-SMS接口管理功能中下載。
        public string addSerial { get; set; }           //擴展碼,根據向移動公司申請的通道填寫,如果申請的精確匹配通道,則填寫空字符串(""),否則添加移動公司允許的擴展碼。
        public string mac { get; set; }         //API輸入參數簽名結果,簽名算法:將ecName,apId,secretKey,mobiles,content ,sign,addSerial按照順序拼接,然后通過md5(32位小寫)計算后得出的值。
    }

    /**
     * 發送短信響應實體
     */
    public class SendRes
    {
        public string rspcod { get; set; } //響應狀態碼
        public string msgGroup { get; set; }    //消息批次號,由雲MAS平台生成,用於驗證短信提交報告和狀態報告的一致性(取值msgGroup)注:如果數據驗證不通過msgGroup為空
        public bool success { get; set; }    //數據校驗結果
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM