C#發送短信到手機


看了https://blog.csdn.net/whl632359961/article/details/73468115這位朋友的博客,自己測試下,遇到問題了,又重新寫了下,以便以后用到回顧

1. 從網上(http://sms.webchinese.cn/)申請賬號,記住用戶名,密碼會發到手機上,這僅是登陸密碼。

2:短信秘鑰獲得方法 ,后面要用到的,注意獲取短信密鑰時,要按照格式寫好簽名格式:[公司名稱或者網站名稱]

 

3:具體實現,要參考SMS短信通API下行接口參數(http://sms.webchinese.cn/api.shtml),這個網頁上就有各種語言的實現方式,我用C#實現,熟悉java的可以用java。

 

 

4:代碼實現

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 發送短信
{
    public partial class Form1 : Form
    {
        private string url = "http://utf8.sms.webchinese.cn/?";//發送短信平台網址SMS
        private string strUid = "Uid=";//注冊的SMS平台的賬號ID
        private string strKey = "&key=d41d8cd98f00b204e980";//注冊的SMS平台的接口密匙
        private string strMob = "&smsMob=";//手機號碼
        private string strContent = "&smsText=";// 發送的內容


        public Form1()
        {
            InitializeComponent();
        }

        private void BtnSendMsg_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtUserName.Text) && !string.IsNullOrEmpty(txtAttnNum.Text)
                && !string.IsNullOrEmpty(txtContent.Text))
                {
                 url = url + strUid + txtUserName.Text + strKey + strMob + txtAttnNum.Text + strContent + txtContent.Text;
                 string Result = GetHtmlFromUrl(url);
                 MessageBox.Show(Result);
                }
        }

        private string GetHtmlFromUrl(string url)
        {
            string strRet = null;
            if (string.IsNullOrEmpty(url))
            {
                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)
            {
 
                strRet = null;
            }
            return strRet;
        }
    }
}

效果圖:(發送成功返回的是:發送短信的數量)

 

 發送失敗,返回的錯誤碼見http://sms.webchinese.cn/api.shtml

 


免責聲明!

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



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