Esempio n. 1
0
        public void PutImagesIntoView_adds_linked_resources()
        {
            var embedder = new ImageEmbedder(s => new LinkedResource(new MemoryStream()));
            var cid      = embedder.AddImage("test.png");

            using (var view = AlternateView.CreateAlternateViewFromString("<img src=\"cid:" + cid.ContentId + "\" />", new ContentType("text/html")))
            {
                embedder.PutImagesIntoView(view);

                view.LinkedResources.Count.ShouldEqual(1);
                view.LinkedResources[0].ShouldBeSameAs(cid);
            }
        }
Esempio n. 2
0
        AlternateView CreateAlternativeView(Email email, string alternativeViewName)
        {
            var fullViewName  = GetAlternativeViewName(email, 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))
            {
                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
            }
            imageEmbedder.PutImagesIntoView(alternativeView);
            email.ViewData.Remove("Postal.ImageEmbedder");
            return(alternativeView);
        }
Esempio n. 3
0
        public void PutImagesIntoView_adds_linked_resources()
        {
            var embedder = new ImageEmbedder(s => new LinkedResource(new MemoryStream()));
            var cid = embedder.AddImage("test.png");
            using (var view = AlternateView.CreateAlternateViewFromString("<img src=\"cid:" + cid.ContentId + "\" />", new ContentType("text/html")))
            {
                embedder.PutImagesIntoView(view);

                view.LinkedResources.Count.ShouldEqual(1);
                view.LinkedResources[0].ShouldBeSameAs(cid);
            }
        }