private AlternateView CreateAlternativeView(Template template, string alternativeViewName)
        {
            var fullViewName = GetAlternativeViewName(template, alternativeViewName);
            var output       = _alternativeViewRenderer.Render(template, fullViewName);

            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
            }
            template.ImageEmbedder.AddImagesToView(alternativeView);
            return(alternativeView);
        }
Beispiel #2
0
        /// <summary>
        ///     Renders the Template view and builds a <see cref="MailMessage" />. Does not send the Template.
        /// </summary>
        /// <param name="emailTemplate">The Template to render.</param>
        /// <returns>A <see cref="MailMessage" /> containing the rendered Template.</returns>
        private MailMessage ToMailMessage(EmailTemplate emailTemplate)
        {
            var content = _renderer.Render(emailTemplate);

            return(_templateParser.Parse(content, emailTemplate));  // template to mailmessage (+add attachments)
        }