public EmailPending(EmailFailed ef) : base() { this.ID = ef.ID; this.CompanyID = ef.CompanyID; this.UserID = ef.UserID; this.EmailDraftID = ef.EmailDraftID; this.FromAddress = ef.FromAddress; this.LastRetryAt = ef.LastRetryAt; this.Message = ef.Message; this.NotificationID = ef.NotificationID; this.Retry = ef.Retry; this.Status = "Pending"; this.Subject = ef.Subject; this.TimeToSend = ef.TimeToSend; this.ToAddress = ef.ToAddress; this.RecipientName = ef.RecipientName; this.CC = ef.CC; this.Bcc = ef.Bcc; this.AttachmentFiles = ef.AttachmentFiles; this.Priority = ef.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); } }