Example #1
0
        public EmailFailed(EmailPending ep)
            : base()
        {
            _CCList = new List<string>();
            _BccList = new List<string>();
            _AttachmentFileList = new List<string>();

            this.ID = ep.ID;
            this.CompanyID = ep.CompanyID;
            this.UserID = ep.UserID;

            this.EmailDraftID = ep.EmailDraftID;
            this.FromAddress = ep.FromAddress;
            this.LastRetryAt = ep.LastRetryAt;
            this.Message = ep.Message;
            this.NotificationID = ep.NotificationID;
            this.Retry = ep.Retry;
            this.Status = "Failed";
            this.Subject = ep.Subject;
            this.TimeToSend = ep.TimeToSend;
            this.ToAddress = ep.ToAddress;
            this.RecipientName = ep.RecipientName;
            this.CC = ep.CC;
            this.Bcc = ep.Bcc;
            this.AttachmentFiles = ep.AttachmentFiles;
            this.Priority = ep.Priority;
        }
        private void SendSingleEmail(EmailPending ep)
        {
            bool succeeded = false;
            if (ep == null) return;
            try
            {
                //generate mail
                var mail = new MailMessage(new MailAddress(ep.FromAddress, _EmailSenderName), new MailAddress(ep.ToAddress, ep.RecipientName));
                mail.Subject = StripOffHtmlTags(ep.Subject.Replace('\r', ' ').Replace('\n', ' '));
                _Log.Info("Subject in real mail: " + mail.Subject);
                if (ep.Retry > 1) _Log.Debug("Retry mail attachmentFiles: " + ep.AttachmentFiles);
                mail.Body = ep.Message;
                if (IsHtml(mail.Body))
                    mail.IsBodyHtml = true;

                mail.Priority = ep.Priority == 0 ? MailPriority.Low : ep.Priority == 2 ? MailPriority.High : MailPriority.Normal;

                //Add attachment if any
                if (ep.AttachmentFileList != null && ep.AttachmentFileList.Count > 0)
                {
                    foreach (string s in ep.AttachmentFileList)
                    {
                        var attData = new Attachment(s);
                        mail.Attachments.Add(attData);
                    }
                }

                //Add CC if any, assume CC contains email address only
                if (ep.CCList != null && ep.CCList.Count > 0)
                {
                    foreach (string s in ep.CCList)
                    {
                        mail.CC.Add(new MailAddress(s));
                    }
                }
                //Add Bcc if any, assume Bcc contains email address only
                if (ep.BccList != null && ep.BccList.Count > 0)
                {
                    foreach (string s in ep.BccList)
                    {
                        mail.Bcc.Add(new MailAddress(s));
                    }
                }

                //send the mail
                if (_SmtpClient != null && !_SuppressNotification)
                {
                    _Log.InfoFormat("Sending email {0}", mail.Body);
                    _SmtpClient.Send(mail);
                }
                succeeded = true;
                //mail.Dispose();
            }
            catch (Exception ex)
            {
                var msg = string.Format("Email ID={0} subject=[{1}] to=[{2}]: {3}", ep.ID, ep.Subject.Truncate(30), ep.ToAddress, ex.Message);
                AttentionUtils.Attention(new Guid("480EBC76-9EAF-BADD-823A-E8BF83D2A518"), msg);
                _Log.Warn(msg);
                succeeded = false;
            }
            //if send successfully, save to EmailSent and delete from EmailPending
            if (succeeded)
            {
                DeleteEmailPending(new IDRequest(ep.ID));
                var req = new SaveRequest<EmailSent>();
                var es = new EmailSent(ep);
                es.LastRetryAt = ep.DateModified;
                es.Retry++;
                es.TimeToSend = DateTime.MaxValue;//never to send again
                req.Item = es;
                SaveEmailSent(req);

            }
            //if failed, save to EmailFailed and delete from EmailPending
            else
            {
                DeleteEmailPending(new IDRequest(ep.ID));
                var request = new SaveRequest<EmailFailed>();
                var es = new EmailFailed(ep);
                if (es.ToAddress.HasValidEmailAddress() && es.Retry < _Retries.Length)
                {
                    es.TimeToSend = DateTime.UtcNow.AddMinutes(_Retries[es.Retry]);
                    es.Retry++;
                }
                else
                {
                    es.TimeToSend = DateTime.MaxValue; // don't send again
                    es.Deleted = true;
                }
                request.Item = es;
                SaveEmailFailed(request);
            }
        }
        private void CheckFailedEmails()
        {
            try
            {
                var response = GetEmailFailedDueList(new IDRequest(Guid.Empty));
                if (response.List != null && response.List.Count > 0)
                {
                    SaveListRequest<EmailPending> request = new SaveListRequest<EmailPending>();
                    request.List = new List<EmailPending>();
                    foreach (EmailFailed ef in response.List)
                    {
                        //put all due failed emails to pending
                        EmailPending ep = new EmailPending(ef);

                        request.List.Add(ep);
                        DeleteEmailFailed(new IDRequest(ef.ID));
                    }
                    SaveEmailPendingList(request);//save to db pending table

                }
            }
            catch (Exception e)
            {
                ErrorHandler.HandleInternal(e);
                _Log.Info("NOTIFYError - CheckFailedEmails - " + (e.InnerException != null ? e.InnerException.Message : e.Message));
            }
        }
        private BusinessMessageResponse SendEmailInternal(
			string template,
			string recipientList,
			string fromAddress,
			string subject,
			Guid notifyID,
			string companyID,
			string userID,
			string cc,
			string bcc,
			string attachments,
			string tzid,
			string data,
			byte priority)
        {
            //IM-5884 check recipient list on sending email
            if (null == recipientList || string.IsNullOrEmpty(recipientList))
            {
                var msg = string.Format("{0} - Recipient list cannot be null or empty", MethodBase.GetCurrentMethod().Name);
                return new BusinessMessageResponse { Status = false, StatusMessage = msg };
            }

            string attachmentFiles = SaveAttachmentData(_AttachmentFolder, attachments);
            EmailDraft ed = CreateEmailDraft(template, fromAddress, subject, notifyID, companyID, userID, tzid, data, priority);
            var destinationMap = recipientList.KeyValueMap(ValueFormat.Strings, true);
            foreach (string recipientName in destinationMap.Keys)
            {
                EmailPending ep = new EmailPending(ed);
                ep.ToAddress = (string)destinationMap[recipientName];
                ep.RecipientName = recipientName == ep.ToAddress ? string.Empty : recipientName.ToString();
                ep.Message = ed.Message;
                ep.CC = cc;
                ep.Bcc = bcc;
                ep.AttachmentFiles = attachmentFiles;
                ep.Priority = priority;
                SaveEmailPending(new SaveRequest<EmailPending>(ep));//save to db as pending email
            }

            return new BusinessMessageResponse();
        }
        private void SendSMSInternal(string template, string recipientList, string fromAddress, string subject, Guid notifyID, string companyID, string userID, string tzid, string data)
        {
            EmailDraft ed = CreateEmailDraft(template, fromAddress, subject, notifyID, companyID, userID, tzid, data, 0);
            var destinationMap = recipientList.KeyValueMap(ValueFormat.Strings, true);
            foreach (var recipientName in destinationMap.Keys)
            {
                EmailPending ep = new EmailPending(ed);
                ep.ToAddress = (string)destinationMap[recipientName] + _SMSReceiverSuffix;
                ep.RecipientName = (string)recipientName == ep.ToAddress ? string.Empty : recipientName.ToString();
                ep.Message = ed.Message;
                ep.CC = "";
                ep.Bcc = "";
                ep.AttachmentFiles = "";
                SaveEmailPending(new SaveRequest<EmailPending>(ep));//save to db as pending email
            }

            //SMSDraft sd = CreateSMSDraft(template, subject, notifyID, companyID, userID, data);
            //var destinationMap = recipientList.KeyValueMap(ValueFormat.Strings, true);
            //foreach (var recipientName in destinationMap.Keys)
            //{
            //  SMSPending sp = new SMSPending(sd);
            //  //sp.Message = TranslateTemplate(template, recipientName);
            //  sp.Message = sd.Message;
            //  sp.Subject = subject;
            //  sp.ToPhoneNumber = (string)destinationMap[recipientName];
            //  sp.RecipientName = recipientName.ToString();
            //  //Todo: add greetings to message.
            //  SaveSMSPending(new SaveRequest<SMSPending>(sp));//save to db as pending SMS
            //}
        }