手机发送短信

  1 public class Phone
  2 {
  3 /// <summary>
  4 /// 日志
  5 /// </summary>
  6 public static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  7 
  8 
  9 public static readonly string ServerAddress = ConfigurationManager.AppSettings["PhoneServerAddress"];
 10 public static readonly string Account = ConfigurationManager.AppSettings["PhoneUserName"];
 11 public static readonly string Pswd = ConfigurationManager.AppSettings["PhonePassword"];
 12 
 13 /// <summary>
 14 /// 发送手机短信
 15 /// </summary>
 16 /// <param name="mobile">手机号</param>
 17 /// <param name="content">内容</param>
 18 /// <param name="sign">签名</param>
 19 /// <returns></returns>
 20 public static string Send(string mobile, string content, string sign = null)
 21 {
 22 //发信内容要做一下Url编码
 23 content = HttpUtility.UrlEncode(content + sign);
 24 
 25 var regPhone = new Regex("^[0-9]+[-]?[0-9]+[-]?[0-9]$");
 26 
 27 if (regPhone.IsMatch(mobile) && !string.IsNullOrEmpty(content))
 28 {
 29 string postString = "account={0}&pswd={1}&mobile={2}&msg={3}&needstatus=true&product={4}&extno={5}".FormatMe(Account, Pswd, mobile, content, null, sign);
 30 
 31 var hh = new HttpExtend();
 32 
 33 var result = hh.PostWebRequest(ServerAddress, postString);
 34 
 35 var returnResult = result.Split(',')[1].Split('\n')[0];
 36 if (returnResult != "0")
 37 {
 38 Log.Error("短信网关发生错误 错误代码{0},MobilePhone{1}".FormatMe(result, "******" + mobile.Substring(6)));
 39 }
 40 
 41 ////*
 42 //如果returnResult值等于0,表示发送成功
 43 
 44 //发送返回参数说明:
 45 //返回代码    代码说明
 46 //0 提交成功
 47 //101 无此用户
 48 //102 密码错
 49 //103 提交过快(提交速度超过流速限制)
 50 //104 系统忙(因平台侧原因,暂时无法处理提交的短信)
 51 //105 敏感短信(短信内容包含敏感词)
 52 //106 消息长度错(> 536或 <= 0)
 53 //107 包含错误的手机号码
 54 //108 手机号码个数错(群发 > 50000或 <= 0; 单发 > 200或 <= 0)
 55 //109 无发送额度(该用户可用短信数已使用完)
 56 //110 不在发送时间内
 57 //111 超出该账户当月发送额度限制
 58 //112 无此产品,用户没有订购该产品
 59 //113 extno格式错(非数字或者长度不对)
 60 //115 自动审核驳回
 61 //116 签名不合法,未带签名(用户必须带签名的前提下)
 62 //117 IP地址认证错,请求调用的IP地址不是系统登记的IP地址
 63 //118 用户没有相应的发送权限
 64 //119 用户已过期
 65 //120 测试内容不是白名单
 66 
 67 return returnResult;
 68 }
 69 else
 70 {
 71 return "103";
 72 }
 73 }
 74 
 75 /// <summary> 短信群发,号码间用英文逗号分隔
 76 /// </summary>
 77 /// <param name="mobiles">例如:"15189799192,15189799192"</param>
 78 /// <param name="content">群发短信内容</param>
 79 /// <returns></returns>
 80 public static string GroupSend(string mobiles, string content)
 81 {
 82 //发信内容要做一下Url编码
 83 content = HttpUtility.UrlEncode(content);
 84 string result = string.Empty;
 85 if (!string.IsNullOrEmpty(content))
 86 {
 87 var postString = "account={0}&pswd={1}&mobile={2}&msg={3}&needstatus=true&product={4}&extno={5}".FormatMe(Account, Pswd, mobiles, content, null, null);
 88 var hh = new HttpExtend();
 89 
 90 result = hh.PostWebRequest(ServerAddress, postString);
 91 
 92 //*
 93 //如果returnResult值等于0,表示发送成功
 94 
 95 //发送返回参数说明:
 96 //返回代码    代码说明
 97 //0 提交成功
 98 //101 无此用户
 99 //102 密码错
100 //103 提交过快(提交速度超过流速限制)
101 //104 系统忙(因平台侧原因,暂时无法处理提交的短信)
102 //105 敏感短信(短信内容包含敏感词)
103 //106 消息长度错(> 536或 <= 0)
104 //107 包含错误的手机号码
105 //108 手机号码个数错(群发 > 50000或 <= 0; 单发 > 200或 <= 0)
106 //109 无发送额度(该用户可用短信数已使用完)
107 //110 不在发送时间内
108 //111 超出该账户当月发送额度限制
109 //112 无此产品,用户没有订购该产品
110 //113 extno格式错(非数字或者长度不对)
111 //115 自动审核驳回
112 //116 签名不合法,未带签名(用户必须带签名的前提下)
113 //117 IP地址认证错,请求调用的IP地址不是系统登记的IP地址
114 //118 用户没有相应的发送权限
115 //119 用户已过期
116 //120 测试内容不是白名单
117 }
118 return result;
119 }
120 }

 

posted @ 2017-01-20 10:30  小小的菜鸟程序员  阅读(513)  评论(0编辑  收藏  举报