/// <summary> /// /// </summary> /// <param name="messageTemplate"></param> /// <param name="emailAccount"></param> /// <param name="languageId"></param> /// <param name="tokens"></param> /// <param name="toEmailAddress"></param> /// <param name="toName"></param> /// <param name="attachmentFilePath"></param> /// <param name="attachmentFileName"></param> /// <param name="replyToEmailAddress"></param> /// <param name="replyToName"></param> /// <param name="fromEmail"></param> /// <param name="fromName"></param> /// <param name="subject"></param> /// <returns>Retuern QueuedEmail ID</returns> public static int SendNotification(int CompanyID, DNHMessageTemplate messageTemplate, EmailAccount emailAccount, IEnumerable <Token> tokens, string toEmailAddress, string toName, string attachmentFilePath = null, string attachmentFileName = null, string replyToEmailAddress = null, string replyToName = null, string fromEmail = null, string fromName = null, string subject = null) { if (messageTemplate == null) { throw new ArgumentNullException("messageTemplate"); } if (emailAccount == null) { throw new ArgumentNullException("emailAccount"); } Tokenizer _tokenizer = new Tokenizer(new MessageTemplatesSettings()); //retrieve localized message template data var bcc = messageTemplate.BccEmailAddresses; if (String.IsNullOrEmpty(subject)) { subject = messageTemplate.Subject; } var body = messageTemplate.Body; //Replace subject and body tokens var subjectReplaced = _tokenizer.Replace(subject, tokens, false); var bodyReplaced = _tokenizer.Replace(body, tokens, false); //limit name length toName = CommonHelper.EnsureMaximumLength(toName, 300); var email = new QueuedEmail { CompanyID = CompanyID, Priority = QueuedEmailPriority.High, From = !string.IsNullOrEmpty(fromEmail) ? fromEmail : emailAccount.Email, FromName = !string.IsNullOrEmpty(fromName) ? fromName : emailAccount.DisplayName, To = toEmailAddress, ToName = toName, ReplyTo = replyToEmailAddress, ReplyToName = replyToName, CC = string.Empty, Bcc = bcc, Subject = subjectReplaced, Body = bodyReplaced, AttachmentFilePath = attachmentFilePath, AttachmentFileName = attachmentFileName, AttachedDownloadId = messageTemplate.AttachedDownloadId, CreatedOnUtc = DateTime.UtcNow, CreatedDate = SystemConfig.CurrentDate, ///EmailAccountId = emailAccount.Id, DontSendBeforeDateUtc = !messageTemplate.DelayBeforeSend.HasValue ? null : (DateTime?)(DateTime.UtcNow + TimeSpan.FromHours(messageTemplate.DelayPeriod.ToHours(messageTemplate.DelayBeforeSend.Value))) }; QueuedEmailManager.Add(email); return(email.Id); }
/// <summary> /// Executes a task /// </summary> public virtual void Execute() { var maxTries = 3; var queuedEmails = QueuedEmailManager.GetUnSendingEmail(); //new QueuedEmailCollection(); //_queuedEmailService.SearchEmails(fromEmail, toEmail, createdFromUtc, createdToUtc, //loadNotSentItemsOnly=true, loadOnlyItemsToBeSent=true, maxTries, loadNewest=false, 0, 500); // IPagedList<QueuedEmail> SearchEmails(string fromEmail, //string toEmail, DateTime? createdFromUtc, DateTime? createdToUtc, //bool loadNotSentItemsOnly, bool loadOnlyItemsToBeSent, int maxSendTries, //bool loadNewest, int pageIndex = 0, int pageSize = int.MaxValue); foreach (var queuedEmail in queuedEmails) { var bcc = String.IsNullOrWhiteSpace(queuedEmail.Bcc) ? null : queuedEmail.Bcc.Split(new [] { ';' }, StringSplitOptions.RemoveEmptyEntries); var cc = String.IsNullOrWhiteSpace(queuedEmail.CC) ? null : queuedEmail.CC.Split(new [] { ';' }, StringSplitOptions.RemoveEmptyEntries); try { EmailSender.SendEmail(null, queuedEmail.Subject, queuedEmail.Body, queuedEmail.From, queuedEmail.FromName, queuedEmail.To, queuedEmail.ToName, queuedEmail.ReplyTo, queuedEmail.ReplyToName, bcc, cc, queuedEmail.AttachmentFilePath, queuedEmail.AttachmentFileName, queuedEmail.AttachedDownloadId); queuedEmail.SentOnUtc = DateTime.UtcNow; } catch (Exception exc) { // _logger.Error(string.Format("Error sending e-mail. {0}", exc.Message), exc); } finally { queuedEmail.SentTries = queuedEmail.SentTries + 1; QueuedEmailManager.Update(queuedEmail); } } }