예제 #1
0
        public TemplatedEmail Generate <TModel>(TModel model, string templateName = null)
        {
            var language = GetCurrentLanguage();

            var templatedEmail = new TemplatedEmail();

            if (String.IsNullOrEmpty(templateName))
            {
                templateName = typeof(TModel).Name;
            }

            if (language.Length > 0)
            {
                templateName = "{0}.{1}".FormatWith(language, templateName);
            }

            var subjectTemplate       = _templateService.Resolve("{0}\\Subject.cshtml".FormatWith(templateName), model);
            var plainTextBodyTemplate = _templateService.Resolve("{0}\\PlainText.cshtml".FormatWith(templateName), model);
            var htmlBodyTemplate      = _templateService.Resolve("{0}\\Html.cshtml".FormatWith(templateName), model);

            templatedEmail.Subject = _templateService.Run(subjectTemplate, null).HtmlEntityDecode().RemoveLineFeeds();
            dynamic viewBag = new DynamicViewBag();

            viewBag.Subject = templatedEmail.Subject;
            templatedEmail.PlainTextBody = ((string)_templateService.Run(plainTextBodyTemplate, viewBag)).HtmlEntityDecode();
            templatedEmail.HtmlBody      = _templateService.Run(htmlBodyTemplate, viewBag);

            return(templatedEmail);
        }
예제 #2
0
 public MailMessage BuildMailMessageFrom(TemplatedEmail templatedEmail, DynamicViewBag viewBag = null)
 {
     if (viewBag == null)
     {
         viewBag = new DynamicViewBag();
     }
     return(BuildMailMessageFrom(LayoutTemplatedEmail(templatedEmail, viewBag)));
 }
예제 #3
0
        public TemplatedEmailAfterLayout LayoutTemplatedEmail(TemplatedEmail templatedEmail, DynamicViewBag viewBag = null)
        {
            var templatedEmailAfterLayout = new TemplatedEmailAfterLayout();

            if (viewBag == null)
            {
                viewBag = new DynamicViewBag();
            }

            templatedEmailAfterLayout.Subject   = templatedEmail.Subject;
            templatedEmailAfterLayout.PlainText = LayoutPlainTextContent(templatedEmail, viewBag);
            templatedEmailAfterLayout.Html      = LayoutHtmlContent(templatedEmail, viewBag);

            return(templatedEmailAfterLayout);
        }
예제 #4
0
        public MailMessage GenerateMessage(TemplatedEmail templatedEmail)
        {
            // Create the mail message and set the subject
            var mailMessage = new MailMessage {
                Subject = templatedEmail.Subject
            };

            var plainTextView = AlternateView.CreateAlternateViewFromString(templatedEmail.PlainTextBody, null, "text/plain");
            var htmlView      = AlternateView.CreateAlternateViewFromString(templatedEmail.HtmlBody, null, "text/html");

            // Add the views
            mailMessage.AlternateViews.Add(plainTextView);
            mailMessage.AlternateViews.Add(htmlView);

            return(mailMessage);
        }
예제 #5
0
        public TemplatedEmail BuildTemplatedEmailFrom <TModel>(TModel model, DynamicViewBag viewBag = null)
        {
            var language = EnsureCurrentLanguageScope();

            var templatedEmail = new TemplatedEmail();

            if (viewBag == null)
            {
                viewBag = new DynamicViewBag();
            }

            // Parse the email subject and cleanse newline characters (otherwise the email class will throw)
            templatedEmail.Subject = HttpUtility.HtmlDecode(_templateService.Run(_emailTemplateInitializer.GetSubjectTemplateName(typeof(TModel), language), model, viewBag: viewBag)).CleanseCRLF();

            // Parse the body in both formats which will be wrapped in the layouts in a separate step
            templatedEmail.PlainTextBody = HttpUtility.HtmlDecode(_templateService.Run(_emailTemplateInitializer.GetPlainTextTemplateName(typeof(TModel), language), model, viewBag: viewBag));
            templatedEmail.HtmlBody      = _templateService.Run(_emailTemplateInitializer.GetHtmlTemplateName(typeof(TModel), language), model, viewBag: viewBag);

            return(templatedEmail);
        }
예제 #6
0
        private string LayoutHtmlContent(TemplatedEmail templatedEmail, DynamicViewBag viewBag = null)
        {
            var language = EnsureCurrentLanguageScope();

            return(_templateService.Run(_emailTemplateInitializer.GetHtmlLayoutName(language), templatedEmail.HtmlBody, viewBag: viewBag));
        }