예제 #1
0
        public SMSSent(SMSPending sp)
            : base()
        {
            this.ID = SequentialGuid.NewDbGuid();
            this.CompanyID = sp.CompanyID;
            this.UserID = sp.UserID;

            this.LastRetryAt = sp.LastRetryAt;
            this.Subject = sp.Subject;
            this.Message = sp.Message;
            this.NotificationID = sp.NotificationID;
            this.RecipientName = sp.RecipientName;
            this.Retry = sp.Retry;
            this.SMSDraftID = sp.SMSDraftID;
            this.Status = "Sent";
            this.TimeToSend = sp.TimeToSend;
            this.ToPhoneNumber = sp.ToPhoneNumber;
        }
        //. IM-3927
        private void CheckFailedSMS()
        {
            try
            {
                var response = GetSMSFailedDueList(new IDRequest(Guid.Empty));
                if (response.List != null && response.List.Count > 0)
                {
                    SaveListRequest<SMSPending> request = new SaveListRequest<SMSPending>();
                    request.List = new List<SMSPending>();
                    foreach (SMSFailed sf in response.List)
                    {
                        //put all due failed emails to pending
                        SMSPending ep = new SMSPending(sf);

                        request.List.Add(ep);
                        DeleteSMSFailed(new IDRequest(sf.ID));
                    }
                    SaveSMSPendingList(request);//save to db pending table
                }
            }
            catch (Exception e)
            {
                ErrorHandler.HandleInternal(e);
                _Log.Info("NOTIFYError - CheckFailedSMSs - " + (e.InnerException != null ? e.InnerException.Message : e.Message));
            }
        }
        private void SendSingleSMS(SMSPending sp)
        {
            //send SMS as email
            bool succeeded = false;
            if (sp == null) return;
            try
            {
                //generate mail
                MailMessage mail = new MailMessage(new MailAddress(_SMSSenderAddress, _EmailSenderName), new MailAddress(sp.ToPhoneNumber + _SMSReceiverSuffix));
                mail.Subject = StripOffHtmlTags(sp.Subject);
                mail.Body = StripOffHtmlTags(sp.Message);

                //send the mail
                if (_SmtpClient != null && !_SuppressNotification)
                {
                    _SmtpClient.Send(mail);
                }
                succeeded = true;
                //mail.Dispose();
            }
            catch (FormatException ex)
            {
                _Log.Info(ex.Message);
                succeeded = false;
            }
            catch (SmtpException ex)
            {
                _Log.Info(ex.Message);
                succeeded = false;
            }
            catch (Exception e)
            {
                _Log.Info(e.Message);
                succeeded = false;
            }
            //if send successfully, save to SMSSent and delete from SMSPending
            if (succeeded)
            {
                SaveRequest<SMSSent> req = new SaveRequest<SMSSent>();
                SMSSent ss = new SMSSent(sp);
                ss.LastRetryAt = sp.DateModified;
                ss.Retry = sp.Retry + 1;
                ss.TimeToSend = DateTime.MaxValue;//never to send again
                req.Item = ss;
                SaveSMSSent(req);
                DeleteSMSPending(new IDRequest(sp.ID));

            }
            //if failed, save to SMSFailed and delete from SMSPending
            else
            {
                SaveRequest<SMSFailed> request = new SaveRequest<SMSFailed>();
                SMSFailed sf = new SMSFailed(sp);
                sf.LastRetryAt = sp.DateModified;
                if (!string.IsNullOrEmpty(sf.ToPhoneNumber) && sf.Retry < _Retries.Length)
                {
                    sf.TimeToSend = DateTime.UtcNow.AddMinutes(_Retries[sf.Retry]);
                    sf.Retry++;
                }
                else
                {
                    sf.TimeToSend = DateTime.MaxValue; // don't send again
                    sf.Deleted = true;
                }
                request.Item = sf;
                SaveSMSFailed(request);
                DeleteSMSPending(new IDRequest(sp.ID));
            }
        }