/// <summary> /// Renders the email view and builds a <see cref="MailMessage"/>. Does not send the email. /// </summary> /// <param name="email">The email to render.</param> /// <returns>A <see cref="MailMessage"/> containing the rendered email.</returns> public MailMessage CreateMailMessage(Email email) { #if ASPNET5 var rawEmailString = emailViewRenderer.Render(email, RequestUrl); emailParser = new EmailParser(emailViewRenderer, RequestUrl); #else var rawEmailString = emailViewRenderer.Render(email, request: Request); #endif var mailMessage = emailParser.Parse(rawEmailString, email); return(mailMessage); }
public MailMessage CreateMailMessage(Email email) { var rawEmailString = _emailViewRenderer.Render(email); var mailMessage = _emailParser.Parse(rawEmailString, email); return(mailMessage); }
AlternateView CreateAlternativeView(Email email, string alternativeViewName) { var fullViewName = GetAlternativeViewName(email, alternativeViewName); #if ASPNET5 var output = alternativeViewRenderer.Render(email, _requsetFeature); #else var output = alternativeViewRenderer.Render(email, fullViewName); #endif string contentType; string body; using (var reader = new StringReader(output)) { contentType = ParseHeadersForContentType(reader); body = reader.ReadToEnd(); } if (string.IsNullOrWhiteSpace(contentType)) { if (alternativeViewName.Equals("text", StringComparison.OrdinalIgnoreCase)) { contentType = "text/plain"; } else if (alternativeViewName.Equals("html", StringComparison.OrdinalIgnoreCase)) { contentType = "text/html"; } else { throw new Exception("The 'Content-Type' header is missing from the alternative view '" + fullViewName + "'."); } } var stream = CreateStreamOfBody(body); var alternativeView = new AlternateView(stream, contentType); if (alternativeView.ContentType.CharSet == null) { // Must set a charset otherwise mail readers seem to guess the wrong one! // Strings are unicode by default in .net. alternativeView.ContentType.CharSet = Encoding.Unicode.WebName; // A different charset can be specified in the Content-Type header. // e.g. Content-Type: text/html; charset=utf-8 } email.ImageEmbedder.AddImagesToView(alternativeView); return(alternativeView); }
private string CreateBody(EmailNotificationRequest request, out bool isHtml) { isHtml = true; if (!string.IsNullOrWhiteSpace(request.ViewName)) { return(_renderer.Render(request)); } if (!string.IsNullOrEmpty(request.HtmlBody?.ToString())) { return(request.HtmlBody.ToString()); } isHtml = false; return(request.Body); }
AlternateView CreateAlternativeView(Email email, string alternativeViewName) { var fullViewName = email.ViewName + "." + alternativeViewName; var imageEmbedder = new ImageEmbedder(); email.ViewData["Postal.ImageEmbedder"] = imageEmbedder; var output = alternativeViewRenderer.Render(email, fullViewName); string contentType = null; string body = null; using (var reader = new StringReader(output)) { contentType = ParseHeadersForContentType(reader); body = reader.ReadToEnd(); } if (string.IsNullOrWhiteSpace(contentType)) { throw new Exception("The 'Content-Type' header is missing from the alternative view '" + fullViewName + "'."); } var stream = CreateStreamOfBody(body); var alternativeView = new AlternateView(stream, contentType); if (alternativeView.ContentType.CharSet == null) { // Must set a charset otherwise mail readers seem to guess the wrong one! // Strings are unicode by default in .net. alternativeView.ContentType.CharSet = Encoding.Unicode.WebName; // A different charset can be specified in the Content-Type header. // e.g. Content-Type: text/html; charset=utf-8 } imageEmbedder.PutImagesIntoView(alternativeView); email.ViewData.Remove("Postal.ImageEmbedder"); return(alternativeView); }