C#实现发送短信到手机

来源:互联网 发布:dcs编程软件 编辑:程序博客网 时间:2024/05/16 12:19

C#实现发送短信到手机

具体实现如下:

1. 从网上(http://sms.webchinese.cn/)申请账号,记住用户名,密码会发到手机上,这仅是登陆密码。里面还有短信秘钥,这个要得到,这是后面要用到的,要在里面写好签名,还有,具体实现,要参考SMS短信通API下行接口参数(http://sms.webchinese.cn/api.shtml),这个网页上就有各种语言的实现方式,我用C#实现,熟悉java的可以用java。

2. 现在就可以编程实现了,这个也很简单,参考接口参数网页的C#实现即可,下面给出我的例子!

其界面如下:

这个就是密匙。不是要注册的登录密码,是要受用这个密匙。

注册完成后会有5条免费试用短信,用完后就需要收费(充值)。


代码如下。可以直接复制使用

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Net;using System.IO;namespace sendPhpneMessage{    public partial class Form1 : Form    {        private string url = "http://utf8.sms.webchinese.cn/?";//发送短信平台网址SMS        private string strUid = "Uid=";//注册的SMS平台的账号ID        private string strKey = "&key=0955bbe00e418b6e2e47";//注册的SMS平台的接口密匙        private string strMob = "&smsMob=";//手机号码        private string strContent = "&smsText=";// 发送的内容        public Form1()        {            InitializeComponent();        }        /// <summary>        /// 按钮单机事件        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void button1_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);            }        }        /// <summary>        ///  发送消息机制        /// </summary>        /// <param name="url"></param>        /// <returns></returns>        public 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;        }    }}


这是一些规则。程序下载地址  点击下载,免积分。