Example #1
0
        /// <summary>
        /// 手机短信(单条发送)
        /// </summary>
        /// <param name="mobile">手机号码</param>
        /// <param name="content">短信内容</param>
        /// <param name="pass">短信通道1验证码通道2广告通道</param>
        /// <param name="msg">返回提示信息</param>
        /// <returns>bool</returns>
        public bool Send(string mobile, string content, int pass, out string msg)
        {
            if (string.IsNullOrEmpty(this.username) || string.IsNullOrEmpty(this.password))
            {
                msg = "短信配置参数有误,请完善后再提交!";
                return(false);
            }
            //验证手机号码
            Regex r = new Regex(@"^1(3|4|5|7|8)\d{9}$", RegexOptions.IgnoreCase);

            if (!r.Match(mobile).Success)
            {
                msg = "手机号码格式不正确!";
                return(false);
            }

            bool status = true;

            BLL.sms_log bll = new BLL.sms_log();
            //查询是否超出平台限制
            int thisSafeTotal = bll.GetCurDayCount();

            if (this.safeTotal > 0 && thisSafeTotal > this.safeTotal)
            {
                msg    = "对不起,平台短信发送量已超出最大限制!";
                status = false;
            }

            //查询当前IP是否已经超出限制
            string ip = DTRequest.GetIP();
            int    thisIpSendCount = bll.GetIPCount(ip);

            if (this.ipCount > 0 && thisIpSendCount > this.ipCount)
            {
                msg    = "对不起,你的网络已经超出发送数量限制!";
                status = false;
            }
            msg = string.Empty;
            if (status)
            {
                //发送短信
                Model.sms_log model = new Model.sms_log();
                model.mobile    = mobile;
                model.content   = content;
                model.send_time = DateTime.Now;
                try
                {
                    string result = Utils.HttpPost(apiurl,
                                                   "cmd=tx&pass="******"&uid=" + this.username + "&pwd=" + this.password + "&mobile=" + mobile + "&encode=utf8&content=" + Utils.UrlEncode(content));
                    string[] strArr = result.Split(new string[] { "||" }, StringSplitOptions.None);
                    if (strArr[0] != "100")
                    {
                        status       = false;
                        model.status = 1;
                        model.remark = "提交失败,错误提示:" + strArr[1];
                        msg          = model.remark;
                    }
                    else
                    {
                        model.status = 0;
                        model.remark = strArr[0];
                        msg          = model.remark;
                    }
                }
                catch (Exception ex)
                {
                    status       = false;
                    model.status = 1;
                    model.remark = "提交失败,错误提示:" + ex.Message;
                    msg          = model.remark;
                }
                bll.Add(model);
            }
            //返回状态
            if (status)
            {
                msg = "发送成功!";
                return(true);
            }
            return(false);
        }
Example #2
0
        /// <summary>
        /// 发送手机短信
        /// </summary>
        /// <param name="mobile">手机号码,以英文“,”逗号分隔开</param>
        /// <param name="content">短信内容</param>
        /// <param name="pass">无作用(备用)</param>
        /// <param name="msg">返回提示信息</param>
        /// <returns>bool</returns>
        public bool Send(string mobile, string content, int pass, out string msg)
        {
            if (string.IsNullOrEmpty(this.username) || string.IsNullOrEmpty(this.password) || string.IsNullOrEmpty(apicode))
            {
                msg = "短信配置参数有误,请完善后再提交!";
                return(false);
            }
            //验证手机号码
            Regex r = new Regex(@"^1(3|4|5|7|8)\d{9}$", RegexOptions.IgnoreCase);

            if (!r.Match(mobile).Success)
            {
                msg = "手机号码格式不正确!";
                return(false);
            }

            bool status = true;

            BLL.sms_log bll = new BLL.sms_log();
            //查询是否超出平台限制
            int thisSafeTotal = bll.GetCurDayCount();

            if (this.safeTotal > 0 && thisSafeTotal > this.safeTotal)
            {
                msg    = "对不起,平台短信发送量已超出最大限制!";
                status = false;
            }

            //查询当前IP是否已经超出限制
            string ip = DTRequest.GetIP();
            int    thisIpSendCount = bll.GetIPCount(ip);

            if (this.ipCount > 0 && thisIpSendCount > this.ipCount)
            {
                msg    = "对不起,你的网络已经超出发送数量限制!";
                status = false;
            }
            msg = string.Empty;
            if (status)
            {
                //发送短信
                Model.sms_log model = new Model.sms_log();
                model.mobile    = mobile;
                model.content   = content;
                model.send_time = DateTime.Now;
                try
                {
                    StringBuilder parameter = new StringBuilder();
                    parameter.AppendFormat("SpCode={0}&LoginName={1}&Password={2}&MessageContent={3}", this.apicode, this.username, this.password, content);
                    parameter.AppendFormat("&UserNumber={0}&SerialNumber={1}&ScheduleTime=&ExtendAccessNum=&f=1", mobile, DateTime.Now.ToString("yyyyMMddHHmmssfff") + "001");
                    byte[]     data = Encoding.GetEncoding("GBK").GetBytes(parameter.ToString());
                    HttpHelper http = new HttpHelper();
                    HttpItem   item = new HttpItem()
                    {
                        URL          = "http://sms.api.ums86.com:8899/sms/Api/Send.do",
                        Encoding     = Encoding.Default,
                        Method       = "POST",
                        ContentType  = "application/x-www-form-urlencoded",
                        PostdataByte = data
                    };
                    HttpResult result = http.GetHtml(item);

                    //获取返回值
                    string code = QueryParameter(result.Html, "result");
                    //获取描述
                    string desc = QueryParameter(result.Html, "description");
                    if (string.IsNullOrEmpty(desc))
                    {
                        //加载错误表
                        Dictionary <string, string> dic = ErrorDic();
                        if (dic.ContainsKey(code))
                        {
                            desc = dic[code];
                        }
                    }
                    if (code == "0")
                    {
                        model.status = 0;
                        model.remark = code;
                    }
                    else
                    {
                        model.status = 1;
                        model.remark = desc;
                    }
                }
                catch (Exception ex)
                {
                    status       = false;
                    model.status = 1;
                    model.remark = "提交失败,错误提示:" + ex.Message;
                    msg          = model.remark;
                }
                bll.Add(model);
            }
            //返回状态
            if (status)
            {
                msg = "发送成功!";
                return(true);
            }
            return(false);
        }
Example #3
0
        /// <summary>
        /// 手机短信(批量发送)
        /// 注意:批量发送只验证平台发送总数限制;
        /// </summary>
        /// <param name="mobiles">手机号码,以英文“,”逗号分隔开</param>
        /// <param name="content">短信内容</param>
        /// <param name="pass">短信通道1验证码通道2广告通道</param>
        /// <param name="msg">返回提示信息</param>
        /// <returns>bool</returns>
        public bool MultiSend(string mobiles, string content, int pass, out string msg)
        {
            msg = "";
            if (string.IsNullOrEmpty(this.username) || string.IsNullOrEmpty(this.password))
            {
                msg = "短信配置参数有误,请完善后再提交!";
                return(false);
            }
            int sucCount = 0;

            string[] oldMobileArr = mobiles.Split(',');

            //查询是否超出平台限制
            int thisSafeTotal = new BLL.sms_log().GetCurDayCount();

            if (this.safeTotal > 0 && (thisSafeTotal > this.safeTotal || thisSafeTotal + oldMobileArr.Length > this.safeTotal))
            {
                msg = "对不起,平台短信发送量已超出最大限制!";
                return(false);
            }
            //错误消息
            string errorMsg = string.Empty;
            //验证手机号码
            Regex r = new Regex(@"^1(3|4|5|7|8)\d{9}$", RegexOptions.IgnoreCase);
            //2000条为一批,求出分多少批
            int batch = oldMobileArr.Length / 2000 + 1;

            for (int i = 0; i < batch; i++)
            {
                StringBuilder sb        = new StringBuilder();
                int           sendCount = 0;              //发送数量
                int           maxLenght = (i + 1) * 2000; //循环最大的数

                //检测号码,忽略不合格的,重新组合
                for (int j = 0; j < oldMobileArr.Length && j < maxLenght; j++)
                {
                    int    arrNum = j + (i * 2000);
                    string mobile = oldMobileArr[arrNum].Trim();
                    Match  m      = r.Match(mobile); //搜索匹配项
                    if (m != null)
                    {
                        sendCount++;
                        sb.Append(mobile + ",");
                    }
                }

                //发送短信
                if (sb.ToString().Length > 0)
                {
                    try
                    {
                        string result = Utils.HttpPost(apiurl,
                                                       "cmd=tx&pass="******"&uid=" + this.username + "&pwd=" + this.password + "&mobile=" + Utils.DelLastComma(sb.ToString()) + "&encode=utf8&content=" + Utils.UrlEncode(content));
                        string[] strArr = result.Split(new string[] { "||" }, StringSplitOptions.None);
                        if (strArr[0] != "100")
                        {
                            errorMsg = "提交失败,错误提示:" + strArr[1];
                            continue;
                        }
                        sucCount += sendCount; //成功数量
                    }
                    catch (Exception ex)
                    {
                        errorMsg = "提交失败,错误提示:" + ex.Message;
                    }
                }
            }
            //返回状态
            if (sucCount > 0)
            {
                msg = "成功提交" + sucCount + "条,失败" + (oldMobileArr.Length - sucCount) + "条";
                return(true);
            }
            msg = errorMsg;
            return(false);
        }
Example #4
0
        /// <summary>
        /// 手机短信(批量发送)
        /// 注意:批量发送只验证平台发送总数限制;
        /// </summary>
        /// <param name="mobiles">手机号码,以英文“,”逗号分隔开</param>
        /// <param name="content">短信内容</param>
        /// <param name="pass">无作用(备用)</param>
        /// <param name="msg">返回提示信息</param>
        /// <returns>bool</returns>
        public bool MultiSend(string mobiles, string content, int pass, out string msg)
        {
            msg = "";
            if (string.IsNullOrEmpty(this.username) || string.IsNullOrEmpty(this.password))
            {
                msg = "短信配置参数有误,请完善后再提交!";
                return(false);
            }
            int sucCount = 0;

            string[] oldMobileArr = mobiles.Split(',');

            //查询是否超出平台限制
            int thisSafeTotal = new BLL.sms_log().GetCurDayCount();

            if (this.safeTotal > 0 && (thisSafeTotal > this.safeTotal || thisSafeTotal + oldMobileArr.Length > this.safeTotal))
            {
                msg = "对不起,平台短信发送量已超出最大限制!";
                return(false);
            }
            //错误消息
            string errorMsg = string.Empty;
            //验证手机号码
            Regex r = new Regex(@"^1(3|4|5|7|8)\d{9}$", RegexOptions.IgnoreCase);
            //2000条为一批,求出分多少批
            int batch = oldMobileArr.Length / 2000 + 1;

            for (int i = 0; i < batch; i++)
            {
                StringBuilder sb        = new StringBuilder();
                int           sendCount = 0;              //发送数量
                int           maxLenght = (i + 1) * 2000; //循环最大的数

                //检测号码,忽略不合格的,重新组合
                for (int j = 0; j < oldMobileArr.Length && j < maxLenght; j++)
                {
                    int    arrNum = j + (i * 2000);
                    string mobile = oldMobileArr[arrNum].Trim();
                    Match  m      = r.Match(mobile); //搜索匹配项
                    if (m != null)
                    {
                        sendCount++;
                        sb.Append(mobile + ",");
                    }
                }

                //发送短信
                if (sb.ToString().Length > 0)
                {
                    try
                    {
                        StringBuilder parameter = new StringBuilder();
                        parameter.AppendFormat("SpCode={0}&LoginName={1}&Password={2}&MessageContent={3}", this.apicode, this.username, this.password, content);
                        parameter.AppendFormat("&UserNumber={0}&SerialNumber={1}&ScheduleTime=&ExtendAccessNum=&f=1", Utils.DelLastComma(sb.ToString()), DateTime.Now.ToString("yyyyMMddHHmmssfff") + "001");
                        byte[]     data = Encoding.GetEncoding("GBK").GetBytes(parameter.ToString());
                        HttpHelper http = new HttpHelper();
                        HttpItem   item = new HttpItem()
                        {
                            URL          = "http://sms.api.ums86.com:8899/sms/Api/Send.do",
                            Encoding     = Encoding.Default,
                            Method       = "POST",
                            ContentType  = "application/x-www-form-urlencoded",
                            PostdataByte = data
                        };
                        HttpResult result = http.GetHtml(item);

                        //获取返回值
                        string code = QueryParameter(result.Html, "result");
                        //获取描述
                        errorMsg = QueryParameter(result.Html, "description");
                        if (string.IsNullOrEmpty(errorMsg))
                        {
                            //加载错误表
                            Dictionary <string, string> dic = ErrorDic();
                            if (dic.ContainsKey(code))
                            {
                                errorMsg = dic[code];
                            }
                        }
                        if (code == "0")
                        {
                            sucCount++;
                        }
                    }
                    catch (Exception ex)
                    {
                        errorMsg = "提交失败,错误提示:" + ex.Message;
                    }
                }
            }
            //返回状态
            if (sucCount > 0)
            {
                msg = "成功提交" + sucCount + "条,失败" + (oldMobileArr.Length - sucCount) + "条";
                return(true);
            }
            msg = errorMsg;
            return(false);
        }