Exemplo n.º 1
0
        public static SendResult SendNotification(DateTime startingDate, ConfigParam config)
        {
            SendResult state = new SendResult();
            try
            {
                #region 发送消息
                using (ISession session = new Session())
                {
                    log.Debug("initializing..");
                    OnLoad(session, config); //发送前的初始化工作

                    IList<Notification> todolist = Notification.ToSendList(session, startingDate); //待发送列表
                    log.DebugFormat("{0} messages to send", todolist.Count);
                    if (todolist.Count <= 0) return state;
                    foreach (Notification msg in todolist)
                    {
                        int currentSuccessCount = state.SuccessCount + state.ErrorCount; //对msg的所有接收者发送之前,已经处理过的消息总数量
                        try
                        {
                            log.DebugFormat("to send message {0} ...", msg.NotifyID);
                            msg.ReadyToSend(); //消息通知发送前的预处理
                            NotificationCategory category = Notification.GetCategory(session, msg.CatID);

                            //各个Sender方法的注意点:对每个接收者发送消息时,如果抛出异常则可能导致后面还没有发送的接收者在本次任务时不会发送
                            //抛出异常时catch块中会尝试计算剩余未成功发送的消息数量
                            //如果不抛出异常,则成功时需将state.SuccessCount加1,失败时需要将state.ErrorCount加1
                            if (category.Type == NotificationType.SMS)
                                SMSSender(session, msg, config, state);
                            else if (category.Type == NotificationType.Mail)
                                MailSender(session, msg, config, state);
                            else
                            {
                                log.ErrorFormat("message {0}: type {1} is not supported now", msg.NotifyID, category.Type.ToString());
                                state.ErrorCount += msg.Receivers.Count;
                            }
                        }
                        catch (Exception er)
                        {
                            log.Error("message " + msg.NotifyID.ToString() + ": ", er);
                            state.ErrorCount += (msg.Receivers.Count - (state.ErrorCount + state.SuccessCount - currentSuccessCount));
                        }
                    }
                }
                #endregion
            }
            catch
            {
                throw;
            }
            finally
            {
                log.InfoFormat("{0} messages have been sent, {1} success, {2} failed", state.ErrorCount + state.SuccessCount, state.SuccessCount, state.ErrorCount);
                UnLoad(); //发送结束后释放资源
            }

            return state;
        }
Exemplo n.º 2
0
        private static void SMSSender(ISession session, Notification message, ConfigParam config, SendResult state)
        {
            object s = null;
            string errorMessage = null, mobile;
            bool error = false;
            string errorCode = string.Empty ;
            foreach (NotificationReceiver receiver in message.Receivers)
            {
               s = null;
                error = false;
                errorMessage = null;
                errorCode = "";
                //合法性检查
                mobile = receiver.PostTo;
                if (!CheckSms(ref mobile))
                {
                    log.ErrorFormat("message {0}, receiver: {1} not send: 无效的手机号码", message.NotifyID, receiver.PostTo);
                    errorMessage = "无效的手机号码";
                    error = true;
                }

                //尝试发送
                if (!error)
                    try
                    {
                        s = _smsService.SendMessage(config.SMSAccount, config.SMSPassword, message.Content, mobile);
                        error =_smsService.ParseSendResult(s,out errorCode, out errorMessage);
                        log.DebugFormat("message {0} sent, receiver: {1}, content: {2}", message.NotifyID, mobile, message.Content);
                    }
                    catch (Exception er)
                    {
                        //发送过程如果发生异常,异常信息将尝试记入数据库
                        error = true;
                        errorMessage = er.Message;
                    }

                //记录发送结果状态
                try
                {
                    session.BeginTransaction();
                    bool success = message.AfterSendDo(receiver, error ? "-99998" : "-99999", errorMessage);
                    session.Commit();
                    if (success) state.SuccessCount++;
                    else state.ErrorCount++;
                }
                catch (Exception er)
                {
                    //如果记录发送结果状态时发生异常,只能将异常信息记入日志文件
                    session.Rollback();
                    state.ErrorCount++;
                    log.ErrorFormat("message: {0}, receiver: {1}, result code: {2}, result error: {3}, exception: {4}", message.NotifyID, receiver.PostTo, error?"-99998":errorCode, errorMessage, er.Message);
                }
            }
            message.AfterSendDo();
        }
Exemplo n.º 3
0
        private static void MailSender(ISession session, Notification message, ConfigParam config, SendResult state)
        {
            bool error = false;
            string errorMessage = null, code = null;
            foreach (NotificationReceiver receiver in message.Receivers)
            {
                error = false;
                errorMessage = "";
                code = "0";
                //合法性检查
                if (!CheckMail(receiver.PostTo))
                {
                    log.ErrorFormat("message {0}, receiver: {1} not send: 无效的EMail地址", message.NotifyID, receiver.PostTo);
                    errorMessage = "无效的EMail地址";
                    code = "-99999";
                    error = true;
                }

                //尝试发送
                if (!error)
                    try
                    {
                        _mailService.Subject = message.Title;
                        _mailService.HTMLBody = message.Content;
                        _mailService.ClearRecipients();
                        _mailService.AddRecipient(receiver.PostTo, receiver.UserName, null);
                        error = !_mailService.Send(config.MailServer, false);
                        code = _mailService.ErrorCode.ToString();
                        errorMessage = _mailService.ErrorMessage + " " + _mailService.Log;
                        _mailService.Close();
                        if (error)
                        {
                            log.ErrorFormat("message {0}, receiver: {1} not send: {2} - {3}", message.NotifyID, receiver.PostTo, _mailService.ErrorCode, _mailService.ErrorMessage);
                        }
                        else
                            log.DebugFormat("message {0} sent, receiver: {1}", message.NotifyID, receiver.PostTo);
                    }
                    catch (Exception er)
                    {
                        //发送过程如果发生异常,异常信息将尝试记入数据库
                        error = true;
                        errorMessage = er.Message;
                        code = "-99999";
                    }

                //记录发送结果状态
                try
                {
                    session.BeginTransaction();
                    bool success = message.AfterSendDo(receiver, code, errorMessage);
                    session.Commit();
                    if (success) state.SuccessCount++;
                    else state.ErrorCount++;
                }
                catch (Exception er)
                {
                    //如果记录发送结果状态时发生异常,只能将异常信息记入日志文件
                    session.Rollback();
                    state.ErrorCount++;
                    log.ErrorFormat("message: {0}, receiver: {1}, result code: {2}, result error: {3}, exception: {4}", message.NotifyID, receiver.ReceiverID, code, errorMessage, er.Message);
                }
            }
            message.AfterSendDo();
        }
Exemplo n.º 4
0
        public static SendResult SendNotification(DateTime startingDate, ConfigParam config)
        {
            SendResult state = new SendResult();

            try
            {
                #region 发送消息
                using (ISession session = new Session())
                {
                    log.Debug("initializing..");
                    OnLoad(session, config);                                                        //发送前的初始化工作

                    IList <Notification> todolist = Notification.ToSendList(session, startingDate); //待发送列表
                    log.DebugFormat("{0} messages to send", todolist.Count);
                    if (todolist.Count <= 0)
                    {
                        return(state);
                    }
                    foreach (Notification msg in todolist)
                    {
                        int currentSuccessCount = state.SuccessCount + state.ErrorCount; //对msg的所有接收者发送之前,已经处理过的消息总数量
                        try
                        {
                            log.DebugFormat("to send message {0} ...", msg.NotifyID);
                            msg.ReadyToSend(); //消息通知发送前的预处理
                            NotificationCategory category = Notification.GetCategory(session, msg.CatID);

                            //各个Sender方法的注意点:对每个接收者发送消息时,如果抛出异常则可能导致后面还没有发送的接收者在本次任务时不会发送
                            //抛出异常时catch块中会尝试计算剩余未成功发送的消息数量
                            //如果不抛出异常,则成功时需将state.SuccessCount加1,失败时需要将state.ErrorCount加1
                            if (category.Type == NotificationType.SMS)
                            {
                                SMSSender(session, msg, config, state);
                            }
                            else if (category.Type == NotificationType.Mail)
                            {
                                MailSender(session, msg, config, state);
                            }
                            else
                            {
                                log.ErrorFormat("message {0}: type {1} is not supported now", msg.NotifyID, category.Type.ToString());
                                state.ErrorCount += msg.Receivers.Count;
                            }
                        }
                        catch (Exception er)
                        {
                            log.Error("message " + msg.NotifyID.ToString() + ": ", er);
                            state.ErrorCount += (msg.Receivers.Count - (state.ErrorCount + state.SuccessCount - currentSuccessCount));
                        }
                    }
                }
                #endregion
            }
            catch
            {
                throw;
            }
            finally
            {
                log.InfoFormat("{0} messages have been sent, {1} success, {2} failed", state.ErrorCount + state.SuccessCount, state.SuccessCount, state.ErrorCount);
                UnLoad(); //发送结束后释放资源
            }

            return(state);
        }
Exemplo n.º 5
0
        private static void MailSender(ISession session, Notification message, ConfigParam config, SendResult state)
        {
            bool   error = false;
            string errorMessage = null, code = null;

            foreach (NotificationReceiver receiver in message.Receivers)
            {
                error        = false;
                errorMessage = "";
                code         = "0";
                //合法性检查
                if (!CheckMail(receiver.PostTo))
                {
                    log.ErrorFormat("message {0}, receiver: {1} not send: 无效的EMail地址", message.NotifyID, receiver.PostTo);
                    errorMessage = "无效的EMail地址";
                    code         = "-99999";
                    error        = true;
                }

                //尝试发送
                if (!error)
                {
                    try
                    {
                        _mailService.Subject  = message.Title;
                        _mailService.HTMLBody = message.Content;
                        _mailService.ClearRecipients();
                        _mailService.AddRecipient(receiver.PostTo, receiver.UserName, null);
                        error        = !_mailService.Send(config.MailServer, false);
                        code         = _mailService.ErrorCode.ToString();
                        errorMessage = _mailService.ErrorMessage + " " + _mailService.Log;
                        _mailService.Close();
                        if (error)
                        {
                            log.ErrorFormat("message {0}, receiver: {1} not send: {2} - {3}", message.NotifyID, receiver.PostTo, _mailService.ErrorCode, _mailService.ErrorMessage);
                        }
                        else
                        {
                            log.DebugFormat("message {0} sent, receiver: {1}", message.NotifyID, receiver.PostTo);
                        }
                    }
                    catch (Exception er)
                    {
                        //发送过程如果发生异常,异常信息将尝试记入数据库
                        error        = true;
                        errorMessage = er.Message;
                        code         = "-99999";
                    }
                }

                //记录发送结果状态
                try
                {
                    session.BeginTransaction();
                    bool success = message.AfterSendDo(receiver, code, errorMessage);
                    session.Commit();
                    if (success)
                    {
                        state.SuccessCount++;
                    }
                    else
                    {
                        state.ErrorCount++;
                    }
                }
                catch (Exception er)
                {
                    //如果记录发送结果状态时发生异常,只能将异常信息记入日志文件
                    session.Rollback();
                    state.ErrorCount++;
                    log.ErrorFormat("message: {0}, receiver: {1}, result code: {2}, result error: {3}, exception: {4}", message.NotifyID, receiver.ReceiverID, code, errorMessage, er.Message);
                }
            }
            message.AfterSendDo();
        }
Exemplo n.º 6
0
        private static void SMSSender(ISession session, Notification message, ConfigParam config, SendResult state)
        {
            object s = null;
            string errorMessage = null, mobile;
            bool   error     = false;
            string errorCode = string.Empty;

            foreach (NotificationReceiver receiver in message.Receivers)
            {
                s            = null;
                error        = false;
                errorMessage = null;
                errorCode    = "";
                //合法性检查
                mobile = receiver.PostTo;
                if (!CheckSms(ref mobile))
                {
                    log.ErrorFormat("message {0}, receiver: {1} not send: 无效的手机号码", message.NotifyID, receiver.PostTo);
                    errorMessage = "无效的手机号码";
                    error        = true;
                }

                //尝试发送
                if (!error)
                {
                    try
                    {
                        s     = _smsService.SendMessage(config.SMSAccount, config.SMSPassword, message.Content, mobile);
                        error = _smsService.ParseSendResult(s, out errorCode, out errorMessage);
                        log.DebugFormat("message {0} sent, receiver: {1}, content: {2}", message.NotifyID, mobile, message.Content);
                    }
                    catch (Exception er)
                    {
                        //发送过程如果发生异常,异常信息将尝试记入数据库
                        error        = true;
                        errorMessage = er.Message;
                    }
                }

                //记录发送结果状态
                try
                {
                    session.BeginTransaction();
                    bool success = message.AfterSendDo(receiver, error ? "-99998" : "-99999", errorMessage);
                    session.Commit();
                    if (success)
                    {
                        state.SuccessCount++;
                    }
                    else
                    {
                        state.ErrorCount++;
                    }
                }
                catch (Exception er)
                {
                    //如果记录发送结果状态时发生异常,只能将异常信息记入日志文件
                    session.Rollback();
                    state.ErrorCount++;
                    log.ErrorFormat("message: {0}, receiver: {1}, result code: {2}, result error: {3}, exception: {4}", message.NotifyID, receiver.PostTo, error?"-99998":errorCode, errorMessage, er.Message);
                }
            }
            message.AfterSendDo();
        }