示例#1
0
        private static void SendReportMail(string message)
        {
            string mailto      = Helper.MailTo;
            string mailSubject = Helper.MailSubject;
            string msg         = string.Format("短信发送报告:\r\n", message);

            SmsDA.SendMail(mailto, mailSubject, msg);
        }
示例#2
0
        /// <summary>
        /// 发送手机短信
        /// </summary>
        public static void SendSmsMessage(JobContext context)
        {
            try
            {
                List <SmsEntity> smsList = null;
                SmsDA.WriteLog("发送短信任务开始");
                //获取待发送短信列表
                int topCount = Helper.TopCount;
                int priority = Helper.Priority;

                bool   EnableTranslator = Helper.EnableTranslator;
                string sEncoder         = Helper.SourceEncoder;
                string tEncoder         = Helper.TargetEncoder;

                //晚上只取高有限级的短信
                if (Helper.IsInDayTime())
                {
                    priority = 100;
                }

                smsList = SmsDA.GetSMS2SendList(priority, topCount);

                SmsDA.WriteLog(string.Format("共找到待发送短信记录:{0}条!", smsList.Count));

                if (smsList.Count > 0)
                {
                    SmsDA.WriteLog("开始验证手机号码格式");
                    smsList.ForEach(x =>
                    {
                        x.CheckResult    = Helper.IsCellPhoneNumber(x.CellNumber);
                        x.ProcessMessage = "手机号码格式验证失败!";
                    });

                    //if (EnableTranslator
                    //    || !string.Equals(sEncoder, tEncoder, StringComparison.OrdinalIgnoreCase))
                    //{
                    //    SmsDA.WriteLog(string.Format("开始对短信内容进行编码转换,由{0}转为{1}", sEncoder, tEncoder));
                    //    smsList.ForEach(x =>
                    //    {
                    //        //在所有的短信内容后面加上【易捷网】
                    //        x.SMSContent = x.SMSContent + "【易捷网】";
                    //        x.SMSContent = Encoding.GetEncoding(tEncoder).GetString(Encoding.GetEncoding(sEncoder).GetBytes(x.SMSContent));
                    //    });
                    //}

                    //初始化发短信服务
                    //var smsService = new SendMessage.SmsService.CustomerServicePortTypeClient();
                    ///var smsService = new SendMessage.NetShopService.NetShopServicePortTypeClient();
                    //var smsService = new SmsServiceHttp();
                    //string Account = ConfigurationManager.AppSettings["Account"];
                    //string Password = ConfigurationManager.AppSettings["Password"];

                    string E_CompanyId = ConfigurationManager.AppSettings["E_CompanyId"];
                    string E_UserName  = ConfigurationManager.AppSettings["E_UserName"];
                    string E_PassWord  = ConfigurationManager.AppSettings["E_PassWord"];
                    Random rd          = new Random();
                    Sms    smsService  = new Sms();
                    foreach (var x in smsList)
                    {
                        if (x.CheckResult)
                        {
                            string guid = buildGuid(x.SysNo);

                            string num    = string.Format("{0}{1}", DateTime.Now.ToString("yyyyMMddHHmmssfff"), rd.Next(100, 999));
                            string result = smsService.CallSms(E_CompanyId, E_UserName, E_PassWord, x.SMSContent, x.CellNumber, num, "", "", "");
                            if ((!string.IsNullOrWhiteSpace(result)) && (result.ToLower() == "result=0" ||
                                                                         result.ToLower().IndexOf("&result=0&") > -1 ||
                                                                         result.ToLower().IndexOf("=result=0&") > -1 ||
                                                                         result.ToLower().Substring(0, 9) == "result=0&" ||
                                                                         result.ToLower().Substring(result.Length - 9, 9) == "&result=0" ||
                                                                         result.ToLower().Substring(result.Length - 9, 9) == "=result=0"))
                            {
                                x.ProcessMessage = "短信成功发出! 业务流水号:" + num + ", result=" + result;
                            }
                            else
                            {
                                x.CheckResult    = false;
                                x.ProcessMessage = "短信发送失败! 业务流水号:" + num + ", result=" + result;
                            }
                        }
                    }

                    //更新发送结果
                    foreach (var x in smsList)
                    {
                        if (x.CheckResult)
                        {
                            SmsDA.UpdateResult(x);
                        }
                        else
                        {
                            SmsDA.UpdateRetryCount(x.SysNo);
                        }
                    }

                    StringBuilder rptsb = new StringBuilder();
                    foreach (var x in smsList)
                    {
                        rptsb.AppendFormat("记录编号:{0} - {1}\r\n", x.SysNo, x.ProcessMessage);
                        //SmsDA.WriteLog(string.Format("记录编号:{0} - {1}", x.SysNo,x.ProcessMessage));
                    }

                    SmsDA.WriteLog(string.Format("短信发送报告:{0}", rptsb.ToString()));

                    //邮件通知发送短信报告
                    SendReportMail(rptsb.ToString());
                }
            }
            catch (Exception ex)
            {
                string msg = string.Format("发送短信时出现异常:{0}\r\n{1}", ex.Message, ex.Source);
                SmsDA.WriteLog(msg);
                SendReportMail(msg);
                if (context != null)
                {
                    context.Message += msg + "\r\n";
                }
            }

            SmsDA.WriteLog("本次发送任务结束!");
        }