c#簡單易用的簡訊發送服務 悠逸企業簡訊服務

來源:互聯網
上載者:User

標籤:service   wro   odi   short   發送資料   app   amr   pos   stream   

  悠逸企業簡訊發送服務,是一種比較簡單易操作的簡訊發送服務,使用POST的方式,請求相應地址就可以實現簡訊發送功能
1 /// <summary> 2 /// 簡訊發送服務 3 /// </summary> 4 public class ShortMsgHelper 5 { 6 /// <summary> 7 /// 簡訊服務 帳號 8 /// </summary> 9 private static string uid = ConfigurationManager.AppSettings["ShortMSGUid"]; 10 /// <summary> 11 /// 簡訊服務 密碼 12 /// </summary> 13 private static string pwd = ConfigurationManager.AppSettings["ShortMSGPwd"]; 14 /// <summary> 15 /// 客服 手機號字串 16 /// </summary> 17 private static string CustomerServicePhoneList = ConfigurationManager.AppSettings["CustomerServicePhoneList"]; 18 /// <summary> 19 /// 簡訊服務 簽名(簡訊內容後面加上 此簽名 才能發送成功!形式為:【簽名內容】)如果不加,則發送無效 20 /// </summary> 21 private static string ShortMSGSignature = ConfigurationManager.AppSettings["ShortMSGSignature"]; 22 /// <summary> 23 /// 簡訊服務 開關 24 /// open:開啟 close:關閉 25 /// </summary> 26 private static string ShortMSGSwitch = ConfigurationManager.AppSettings["ShortMSGSwitch"]; 27 28 /// <summary> 29 /// 傳送簡訊開放方法 30 /// </summary> 31 /// <param name="msgContent"></param> 32 /// <returns></returns> 33 public static bool Send(string msgContent,List<string> pList) 34 { 35 //依據簡訊服務開關 36 if (ShortMSGSwitch=="close") 37 { 38 return false; 39 } 40 List<string> phoneList = new List<string>(); 41 if (pList==null||pList.Count==0) 42 { 43 phoneList = CustomerServicePhoneList.Split(new char[] { ‘,‘ }, StringSplitOptions.RemoveEmptyEntries).ToList(); 44 } 45 else 46 { 47 phoneList = pList; 48 } 49 50 return SendMobileMsg(uid, pwd, msgContent, phoneList); 51 } 52 /// <summary> 53 /// 簡訊發送 54 /// </summary> 55 /// <param name="uid">悠逸企業簡訊ID</param> 56 /// <param name="pwd">悠逸企業簡訊密碼</param> 57 /// <param name="msgContent">簡訊內容</param> 58 /// <param name="destListPhones">手機號列表</param> 59 /// <returns></returns> 60 private static bool SendMobileMsg(string uid, string pwd, string msgContent, List<string> destListPhones) 61 { 62 try 63 { 64 bool result = false; 65 string strPhones = string.Join(";", destListPhones.ToArray()); 66 strPhones += ";"; 67 var encoding = System.Text.Encoding.GetEncoding("GB2312"); 68 69 string postData = string.Format("uid={0}&pwd={1}&mobile={2};&msg={3}&dtime=", uid, pwd, strPhones, msgContent + ShortMSGSignature); 70 71 byte[] data = encoding.GetBytes(postData); 72 73 // 定義 WebRequest 74 HttpWebRequest myRequest = 75 (HttpWebRequest)WebRequest.Create("http://www.smsadmin.cn/smsmarketing/wwwroot/api/post_send/"); 76 77 myRequest.Method = "POST"; 78 myRequest.ContentType = "application/x-www-form-urlencoded"; 79 myRequest.ContentLength = data.Length; 80 81 Stream newStream = myRequest.GetRequestStream(); 82 83 //發送資料 84 newStream.Write(data, 0, data.Length); 85 newStream.Close(); 86 87 // 得到 Response 88 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); 89 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default); 90 string content = reader.ReadToEnd(); 91 92 if (content.Substring(0, 1) == "0") 93 result = true; 94 else 95 { 96 if (content.Substring(0, 1) == "2") //餘額不足 97 { 98 //"手機簡訊餘額不足"; 99 //TODO100 }101 else102 {103 //簡訊發送失敗的其他原因,請參看官方API104 }105 result = false;106 }107 108 return result;109 }110 catch111 {112 return false;113 }114 115 }116 }

 

c#簡單易用的簡訊發送服務 悠逸企業簡訊服務

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.