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); }
public MailMessage BuildMailMessageFrom(TemplatedEmail templatedEmail, DynamicViewBag viewBag = null) { if (viewBag == null) { viewBag = new DynamicViewBag(); } return(BuildMailMessageFrom(LayoutTemplatedEmail(templatedEmail, viewBag))); }
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); }
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); }
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); }
private string LayoutHtmlContent(TemplatedEmail templatedEmail, DynamicViewBag viewBag = null) { var language = EnsureCurrentLanguageScope(); return(_templateService.Run(_emailTemplateInitializer.GetHtmlLayoutName(language), templatedEmail.HtmlBody, viewBag: viewBag)); }