一直都用这个平台发手机短信的,今天做新项目的时候用到了,但是上来博客搜索不到,只好翻以前的源代码翻了好久才找到了,先记下来,以作备用:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
namespace Niunan.CardShop.Web.Code
{
public class SendMobile
{
/// <summary>返回发送短信的状态说明
///
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string GetMobileMSMStatus(string str)
{
string s = "";
switch (str)
{
case "-1":
s = "没有该用户账户";
break;
case "-2":
s = "密钥不正确(不是用户密码)";
break;
case "-3":
s = "短信数量不足";
break;
case "-11":
s = "该用户被禁用";
break;
case "-14":
s = "短信内容出现非法字符";
break;
case "-4":
s = "手机号格式不正确";
break;
case "-41":
s = "手机号码为空";
break;
case "-42":
s = "短信内容为空";
break;
default:
s = "成功发送" + str + "条短信";
break;
}
return s;
}
/// <summary>发送手机短信
///
/// </summary>
/// <param name="mobile">手机号码,多个手机号以,号相隔</param>
/// <param name="body">短信内容</param>
public static string SendMobileMSM(string mobile, string body)
{
string url = "http://utf8.sms.webchinese.cn/?Uid=xxxxxx&Key=xxxxxx&smsMob=" + mobile + "&smsText=" + body;
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);
return GetMobileMSMStatus(ser.ReadToEnd());
}
catch (Exception ex)
{
throw ex;
}
}
}
}