C# 實現傳送簡訊

來源:互聯網
上載者:User

標籤:

現在很多網站都是簡訊發送的功能,怎麼實現的呢。對於個人站長來說的話,通過使用SMS簡訊通API介面相對比較划算和簡單。那怎麼實現呢,步驟如下:

1. 從網上(http://sms.webchinese.cn/)申請帳號,記住使用者名稱,密碼會發到手機上,這僅是登陸密碼。註冊後會送5條簡訊、和3條多媒體訊息的發送量。

2.查看SMS簡訊通API下行介面(http://sms.webchinese.cn/api.shtml),然後擷取秘鑰,其實就是加密後的登入密碼。開始敲代碼,相關代碼如下:

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net;using System.Text;using System.Web;namespace Y_PostSms{    public class YMethod    {        private string THE_UID = ""; //使用者名稱        private string THE_KEY = ""; //介面秘鑰        /// <summary>返回UTF-8編碼發送介面地址</summary>        /// <param name="receivePhoneNumber">目的手機號碼(多個手機號請用半形逗號隔開)</param>        /// <param name="receiveSms">簡訊內容,最多支援400個字,普通簡訊70個字/條,長簡訊64個字/條計費</param>        /// <returns></returns>        public string GetPostUrl(string smsMob, string smsText)        {            string postUrl = "http://utf8.sms.webchinese.cn/?Uid=" + THE_UID + "&key=" + THE_KEY + "&smsMob=" + smsMob + "&smsText=" + smsText;            return postUrl;        }        /// <summary> 傳送簡訊,得到傳回值</summary>        public string PostSmsInfo(string url)        {            //調用時只需要把拼成的URL傳給該函數即可。判斷傳回值即可            string strRet = null;            if (url == null || url.Trim().ToString() == "")            {                return strRet;            }            string targeturl = url.Trim().ToString();            try            {                HttpWebRequest hr = (HttpWebRequest)WebRequest.Create(targeturl);                hr.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";                hr.Method = "GET";                hr.Timeout = 30 * 60 * 1000;                WebResponse hs = hr.GetResponse();                Stream sr = hs.GetResponseStream();                StreamReader ser = new StreamReader(sr, Encoding.Default);                strRet = ser.ReadToEnd();            }            catch (Exception ex)            {                strRet = null;            }            return strRet;        }        /// <summary>確認返回資訊 </summary>        public string GetResult(string strRet)        {            int result = 0;            try            {                result = int.Parse(strRet);                switch (result)                {                    case -1:                        strRet = "沒有該使用者賬戶";                        break;                    case -2:                        strRet = "介面密鑰不正確,不是賬戶登陸密碼";                        break;                    case -21:                        strRet = "MD5介面祕密金鑰加密不正確";                        break;                    case -3:                        strRet = "簡訊數量不足";                        break;                    case -11:                        strRet = "該使用者被禁用";                        break;                    case -14:                        strRet = "簡訊內容出現非法字元";                        break;                    case -4:                        strRet = "手機號格式不正確";                        break;                    case -41:                        strRet = "手機號碼為空白";                        break;                    case -42:                        strRet = "簡訊內容為空白";                        break;                    case -51:                        strRet = "簡訊簽名格式不正確,介面簽名格式為:【簽名內容】";                        break;                    case -6:                        strRet = "IP限制";                        break;                    default:                        strRet = "傳送簡訊數量:" + result;                        break;                }            }            catch (Exception ex)            {                strRet = ex.Message;            }            return strRet;        }    }}

 

3. 找線上客服開通發送許可權,填寫好籤名,如下:

 

 

 

原始碼:Y_PostSms.zip

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.