Beispiel #1
0
        public string GenerateEmailBody(PersonModel model)
        {
            // Create a model for our email
            var templateFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin", "EmailTemplates");
            var templateFilePath   = Path.Combine(templateFolderPath, "Email.cshtml");
            // Generate the email body from the template file.
            // 'templateFilePath' should contain the absolute path of your template file.
            //var TemplateService = new TemplateService();
            var emailHtmlBody = TemplateService.Parse(File.ReadAllText(templateFilePath), model, null, "Email");

            /*
             *  Move CSS Inline.  https://github.com/milkshakesoftware/PreMailer.Net
             */
            var pm = new PreMailer.Net.PreMailer(emailHtmlBody);
            //pm.AddAnalyticsTags(source, medium, campaign, content, domain = null); // Optional to add analytics tags
            var result = pm.MoveCssInline();

            return(result.Html);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mailInfo"></param>
        /// <param name="recepients">receivers</param>
        /// <param name="testMode">No mails are sent, generating report for user</param>
        /// <returns>A html formatted report of the send process</returns>
        public override SendMailLog SendEmail(MailInformation mailInfo, JobWorkItems recepients, bool testMode)
        {
            _log.Debug("Starting Send");
            // for logging
            SendMailLog log = new SendMailLog();

            // Need someone to send to
            if (recepients.Items.Count == 0)
            {
                _log.Error("Trying to send newsletter with an empty JobWorkCollection. Please check the collection before attemting to send mail.");
                throw new ArgumentNullException("recepients", "Recipient collection is empty, there is no recipients to send to.");
            }

            // And, we need a sender address
            if (string.IsNullOrEmpty(mailInfo.From))
            {
                _log.Error("Missing from address.");
                throw new ArgumentNullException("mailInfo", "Missing from address.");
            }

            // Inline all css
            PreMailer.Net.PreMailer preMailer = new PreMailer.Net.PreMailer(mailInfo.BodyHtml);
            if (mailInfo.Utm.HasValidUtmCode)
            {
                preMailer.AddAnalyticsTags(mailInfo.Utm.Source, mailInfo.Utm.Medium, mailInfo.Utm.Campaign,
                    mailInfo.Utm.Content);
            }
            InlineResult cssInline = preMailer.MoveCssInline();
            mailInfo.BodyHtml = cssInline.Html;

            // Log any messages, debug is only detected
            // if we have an HttpContext.
            if (IsInDebugMode())
            {
                log.WarningMessages.Add("Premailer CSS warning messages are only shown in debug mode. Primarily for developers.");
                log.WarningMessages.AddRange(cssInline.Warnings.ToArray());
            }

            // Loop through receivers collection, add to collection and send
            // one email per batch size.
            int batchRun = 0;
            int batchSize = GetBatchSize();

            do
            {
                IEnumerable<JobWorkItem> workItems = recepients.Skip(batchRun*batchSize).Take(batchSize);
                int numberofItemsToSend = workItems.Count();
                if (numberofItemsToSend == 0)
                    break;
                batchRun++;

                try
                {
                    if (SendMailBatch(mailInfo, workItems, testMode))
                    {
                        // Mark each item as sent
                        foreach (JobWorkItem workItem in workItems)
                        {
                            log.SuccessMessages.Add(workItem.EmailAddress);
                            // Update status and save it
                            workItem.Status = JobWorkStatus.Complete;
                            // Only save if real work item (could be a test item)
                            if (workItem.JobId > 0)
                                workItem.Save();
                        }
                    }
                }
                catch (Exception ex)
                {
                    _log.Error(string.Format("Error sending batch (to {0} recipients).", recepients.Count()), ex);
                    string exceptionMsg = ex.Message;
                    log.ErrorMessages.Add(exceptionMsg);

                    // Update work item
                    foreach (JobWorkItem workItem in workItems)
                    {
                        workItem.Status = JobWorkStatus.Failed;
                        if (exceptionMsg.Length >= 2000)
                            exceptionMsg = exceptionMsg.Substring(0, 1999);
                        workItem.Info = exceptionMsg;
                        if (workItem.JobId > 0)
                            workItem.Save();
                    }

                    // can't continue
                    break;
                }
            } while (true);

            // Finished
            log.SendStop = DateTime.Now;

            _log.Debug("Ending Send");
            // return report
            return log;
        }
Beispiel #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="mailInfo"></param>
        /// <param name="recepients">receivers</param>
        /// <param name="testMode">No mails are sent, generating report for user</param>
        /// <returns>A html formatted report of the send process</returns>
        public override SendMailLog SendEmail(MailInformation mailInfo, JobWorkItems recepients, bool testMode)
        {
            _log.Debug("Starting Send");
            // for logging
            SendMailLog log = new SendMailLog();

            // Need someone to send to
            if (recepients.Items.Count == 0)
            {
                _log.Error("Trying to send newsletter with an empty JobWorkCollection. Please check the collection before attemting to send mail.");
                throw new ArgumentNullException("recepients", "Recipient collection is empty, there is no recipients to send to.");
            }

            // And, we need a sender address
            if (string.IsNullOrEmpty(mailInfo.From))
            {
                _log.Error("Missing from address.");
                throw new ArgumentNullException("mailInfo", "Missing from address.");
            }

            // Inline all css
            PreMailer.Net.PreMailer preMailer = new PreMailer.Net.PreMailer(mailInfo.BodyHtml);
            if (mailInfo.Utm.HasValidUtmCode)
            {
                preMailer.AddAnalyticsTags(mailInfo.Utm.Source, mailInfo.Utm.Medium, mailInfo.Utm.Campaign,
                                           mailInfo.Utm.Content);
            }
            InlineResult cssInline = preMailer.MoveCssInline();

            mailInfo.BodyHtml = cssInline.Html;

            // Log any messages, debug is only detected
            // if we have an HttpContext.
            if (IsInDebugMode())
            {
                log.WarningMessages.Add("Premailer CSS warning messages are only shown in debug mode. Primarily for developers.");
                log.WarningMessages.AddRange(cssInline.Warnings.ToArray());
            }

            // Loop through receivers collection, add to collection and send
            // one email per batch size.
            int batchRun  = 0;
            int batchSize = GetBatchSize();

            do
            {
                IEnumerable <JobWorkItem> workItems = recepients.Skip(batchRun * batchSize).Take(batchSize);
                int numberofItemsToSend             = workItems.Count();
                if (numberofItemsToSend == 0)
                {
                    break;
                }
                batchRun++;

                try
                {
                    if (SendMailBatch(mailInfo, workItems, testMode))
                    {
                        // Mark each item as sent
                        foreach (JobWorkItem workItem in workItems)
                        {
                            log.SuccessMessages.Add(workItem.EmailAddress);
                            // Update status and save it
                            workItem.Status = JobWorkStatus.Complete;
                            // Only save if real work item (could be a test item)
                            if (workItem.JobId > 0)
                            {
                                workItem.Save();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    _log.Error(string.Format("Error sending batch (to {0} recipients).", recepients.Count()), ex);
                    string exceptionMsg = ex.Message;
                    log.ErrorMessages.Add(exceptionMsg);

                    // Update work item
                    foreach (JobWorkItem workItem in workItems)
                    {
                        workItem.Status = JobWorkStatus.Failed;
                        if (exceptionMsg.Length >= 2000)
                        {
                            exceptionMsg = exceptionMsg.Substring(0, 1999);
                        }
                        workItem.Info = exceptionMsg;
                        if (workItem.JobId > 0)
                        {
                            workItem.Save();
                        }
                    }

                    // can't continue
                    break;
                }
            } while (true);

            // Finished
            log.SendStop = DateTime.Now;

            _log.Debug("Ending Send");
            // return report
            return(log);
        }