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