Exemple #1
0
 /// <summary>Create an Email where the ViewName is derived from the name of the class.</summary>
 /// <remarks>Used when defining strongly typed Email classes.</remarks>
 protected Email()
 {
     Attachments = new List<Attachment>();
     ViewName = DeriveViewNameFromClassName();
     ViewData = new ViewDataDictionary(this);
     ImageEmbedder = new ImageEmbedder();
 }
        public void ReferenceImage_returns_LinkedResource()
        {
            var embedder = new ImageEmbedder(StubLinkedResource);
            var resource = embedder.ReferenceImage("test.png");

            resource.ShouldNotBeNull();
        }
Exemple #3
0
        public void Determine_content_type_from_JPG_file_extension()
        {
            var embedder = new ImageEmbedder(StubLinkedResource);
            var resource = embedder.AddImage("test.jpg");

            resource.ContentType.ShouldEqual(new ContentType("image/jpeg"));
        }
        public void Determine_content_type_from_PNG_http_file_extension()
        {
            var embedder = new ImageEmbedder(StubLinkedResource);
            var resource = embedder.ReferenceImage("http://test.com/test.png");

            resource.ContentType.ShouldBe(new ContentType("image/png"));
        }
Exemple #5
0
 /// <summary>Create an Email where the ViewName is derived from the name of the class.</summary>
 /// <remarks>Used when defining strongly typed Email classes.</remarks>
 protected Email()
 {
     ViewName       = DeriveViewNameFromClassName();
     ViewData       = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary());
     ViewData.Model = this;
     ImageEmbedder  = new ImageEmbedder();
 }
        public void Determine_content_type_from_GIF_file_extension()
        {
            var embedder = new ImageEmbedder(StubLinkedResource);
            var resource = embedder.ReferenceImage("test.gif");

            resource.ContentType.ShouldBe(new ContentType("image/gif"));
        }
Exemple #7
0
 public void Repeated_images_use_the_same_LinkedResource()
 {
     var embedder = new ImageEmbedder(StubLinkedResource);
     var r1 = embedder.ReferenceImage("test-a.png");
     var r2 = embedder.ReferenceImage("test-a.png");
     Assert.Same(r1, r2);
 }
Exemple #8
0
 /// <summary>Create an Email where the ViewName is derived from the name of the class.</summary>
 /// <remarks>Used when defining strongly typed Email classes.</remarks>
 protected Email()
 {
     Attachments   = new List <Attachment>();
     ViewName      = DeriveViewNameFromClassName();
     ViewData      = new ViewDataDictionary(this);
     ImageEmbedder = new ImageEmbedder();
 }
 public void Can_read_image_from_http_url()
 {
     var embedder = new ImageEmbedder();
     using (var resource = embedder.ReferenceImage("http://upload.wikimedia.org/wikipedia/commons/6/63/Wikipedia-logo.png"))
     {
         resource.ContentStream.Length.ShouldNotEqual(0);
     }
 }
        public void Repeated_images_use_the_same_LinkedResource()
        {
            var embedder = new ImageEmbedder(StubLinkedResource);
            var r1       = embedder.ReferenceImage("test-a.png");
            var r2       = embedder.ReferenceImage("test-a.png");

            Assert.Same(r1, r2);
        }
        public void Can_read_image_from_http_url()
        {
            var embedder = new ImageEmbedder();

            using (var resource = embedder.ReferenceImage("http://upload.wikimedia.org/wikipedia/commons/6/63/Wikipedia-logo.png"))
            {
                resource.ContentStream.Length.ShouldNotBe(0);
            }
        }
Exemple #12
0
        /// <summary>
        /// Creates a new Email, that will render the given view.
        /// </summary>
        /// <param name="viewName">The name of the view to render</param>
        public Email(string viewName)
        {
            if (viewName == null) throw new ArgumentNullException("viewName");
            if (string.IsNullOrWhiteSpace(viewName)) throw new ArgumentException("View name cannot be empty.", "viewName");

            Attachments = new List<Attachment>();
            ViewName = viewName;
            ViewData = new ViewDataDictionary(this);
            ImageEmbedder = new ImageEmbedder();
        }
Exemple #13
0
        public void AddImagesToView_adds_linked_resources()
        {
            var embedder = new ImageEmbedder(s => new LinkedResource(new MemoryStream()));
            var cid = embedder.ReferenceImage("test.png");
            using (var view = AlternateView.CreateAlternateViewFromString("<img src=\"cid:" + cid.ContentId + "\" />", new ContentType("text/html")))
            {
                embedder.AddImagesToView(view);

                view.LinkedResources.Count.ShouldEqual(1);
                view.LinkedResources[0].ShouldBeSameAs(cid);
            }
        }
Exemple #14
0
        /// <summary>Create an Email where the ViewName is derived from the name of the class.</summary>
        /// <remarks>Used when defining strongly typed Email classes.</remarks>
        protected Email()
        {
            Attachments = new List <Attachment>();
            ViewName    = DeriveViewNameFromClassName();
#if ASPNET5
            ViewData       = new ViewDataDictionary(new Microsoft.AspNetCore.Mvc.ModelBinding.EmptyModelMetadataProvider(), new Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary());
            ViewData.Model = this;
#else
            ViewData = new ViewDataDictionary(this);
#endif
            ImageEmbedder = new ImageEmbedder();
        }
        public void AddImagesToView_adds_linked_resources()
        {
            var embedder = new ImageEmbedder(s => new LinkedResource(new MemoryStream()));
            var cid      = embedder.ReferenceImage("test.png");

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

                view.LinkedResources.Count.ShouldBe(1);
                view.LinkedResources[0].ShouldBeSameAs(cid);
            }
        }
Exemple #16
0
        public void ReplaceLinkedImagesWithEmbeddedImages_replaces_cid_reference()
        {
            var embedder = new ImageEmbedder();
            var resource = embedder.ReferenceImage("postal.png");

            string body = "<img src=\"cid:" + resource.ContentId + @"""/>";
            var    view = AlternateView.CreateAlternateViewFromString(body);

            embedder.AddImagesToView(view);

            string replaced = TemplateViewResult.ReplaceLinkedImagesWithEmbeddedImages(view, body);

            Assert.DoesNotContain("cid:", replaced);
        }
        public void ReplaceLinkedImagesWithEmbeddedImages_replaces_cid_reference_with_correct_mime()
        {
            var embedder = new ImageEmbedder();
            var resource = embedder.ReferenceImage("postal.png");

            string body = "<img src=\"cid:" + resource.ContentId + @"""/>";
            var    view = AlternateView.CreateAlternateViewFromString(body);

            embedder.AddImagesToView(view);

            string replaced = EmailViewResult.ReplaceLinkedImagesWithEmbeddedImages(view, body);

            Assert.Contains("data:image/png;base64,", replaced);
        }
Exemple #18
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);
        }
Exemple #19
0
        /// <summary>
        /// Creates a new Email, that will render the given view.
        /// </summary>
        /// <param name="viewName">The name of the view to render</param>
        public Email(string viewName)
        {
            if (viewName == null)
            {
                throw new ArgumentNullException("viewName");
            }
            if (string.IsNullOrWhiteSpace(viewName))
            {
                throw new ArgumentException("View name cannot be empty.", "viewName");
            }

            Attachments   = new List <Attachment>();
            ViewName      = viewName;
            ViewData      = new ViewDataDictionary(this);
            ImageEmbedder = new ImageEmbedder();
        }
Exemple #20
0
 public void Can_read_image_from_file_system()
 {
     var embedder = new ImageEmbedder();
     var filename = Path.GetTempFileName();
     try
     {
         File.WriteAllBytes(filename, new byte[] { 42 });
         using (var resource = embedder.ReferenceImage(filename))
         {
             resource.ContentStream.Length.ShouldEqual(1);
         }
     }
     finally
     {
         File.Delete(filename);
     }
 }
Exemple #21
0
        public Email(string viewName, IModelMetadataProvider modelMetadataProvider)
        {
            if (viewName == null)
            {
                throw new ArgumentNullException(nameof(viewName));
            }
            if (string.IsNullOrWhiteSpace(viewName))
            {
                throw new ArgumentException("View name cannot be empty.", "viewName");
            }

            Attachments    = new List <Attachment>();
            ViewName       = viewName;
            ViewData       = new ViewDataDictionary(modelMetadataProvider, new ModelStateDictionary());
            ViewData.Model = this;
            ImageEmbedder  = new ImageEmbedder();
        }
        public void Can_read_image_from_file_system()
        {
            var embedder = new ImageEmbedder();
            var filename = Path.GetTempFileName();

            try
            {
                File.WriteAllBytes(filename, new byte[] { 42 });
                using (var resource = embedder.ReferenceImage(filename))
                {
                    resource.ContentStream.Length.ShouldBe(1);
                }
            }
            finally
            {
                File.Delete(filename);
            }
        }
Exemple #23
0
        public Email(string viewName)
#endif
        {
            if (viewName == null)
            {
                throw new ArgumentNullException(nameof(viewName));
            }
            if (string.IsNullOrWhiteSpace(viewName))
            {
                throw new ArgumentException("View name cannot be empty.", "viewName");
            }

            Attachments = new List <Attachment>();
            ViewName    = viewName;
#if ASPNET5
            _serviceProvider = serviceProvider;
            var modelMetadataProvider = _serviceProvider.GetRequiredService <IModelMetadataProvider>();
            ViewData       = new ViewDataDictionary(new Microsoft.AspNetCore.Mvc.ModelBinding.EmptyModelMetadataProvider(), new Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary());
            ViewData.Model = this;
#else
            ViewData = new ViewDataDictionary(this);
#endif
            ImageEmbedder = new ImageEmbedder();
        }
Exemple #24
0
 string RenderView(IView view, ViewDataDictionary viewData, ControllerContext controllerContext, ImageEmbedder imageEmbedder)
 {
     using (var writer = new StringWriter())
     {
         var viewContext = new ViewContext(controllerContext, view, viewData, new TempDataDictionary(), writer);
         viewData[ImageEmbedder.ViewDataKey] = imageEmbedder;
         view.Render(viewContext, writer);
         viewData.Remove(ImageEmbedder.ViewDataKey);
         return(writer.GetStringBuilder().ToString());
     }
 }
 string RenderView(IView view, ViewDataDictionary viewData, ControllerContext controllerContext, ImageEmbedder imageEmbedder)
 {
     using (var writer = new StringWriter())
     {
         var viewContext = new ViewContext(controllerContext, view, viewData, new TempDataDictionary(), writer);
         viewData[ImageEmbedder.ViewDataKey] = imageEmbedder;
         view.Render(viewContext, writer);
         viewData.Remove(ImageEmbedder.ViewDataKey);
         return writer.GetStringBuilder().ToString();
     }
 }
Exemple #26
0
 public void Determine_content_type_from_GIF_file_extension()
 {
     var embedder = new ImageEmbedder(StubLinkedResource);
     var resource = embedder.ReferenceImage("test.gif");
     resource.ContentType.ShouldEqual(new ContentType("image/gif"));
 }
Exemple #27
0
 public void Determine_content_type_from_PNG_http_file_extension()
 {
     var embedder = new ImageEmbedder(StubLinkedResource);
     var resource = embedder.ReferenceImage("http://test.com/test.png");
     resource.ContentType.ShouldEqual(new ContentType("image/png"));
 }
        public void ReplaceLinkedImagesWithEmbeddedImages_replaces_cid_reference_with_correct_mime()
        {
            var embedder = new ImageEmbedder();
            var resource = embedder.ReferenceImage("postal.png");

            string body = "<img src=\"cid:" + resource.ContentId + @"""/>";
            var view = AlternateView.CreateAlternateViewFromString(body);
            embedder.AddImagesToView(view);
            
            string replaced = EmailViewResult.ReplaceLinkedImagesWithEmbeddedImages(view, body);
            Assert.Contains("data:image/png;base64,", replaced);
        }
Exemple #29
0
 public void ReferenceImage_returns_LinkedResource()
 {
     var embedder = new ImageEmbedder(StubLinkedResource);
     var resource = embedder.ReferenceImage("test.png");
     resource.ShouldNotBeNull();
 }
Exemple #30
0
        public void Can_Read_From_Byte_Array()
        {
            var embedder = new ImageEmbedder();

            using (WebClient webClient = new WebClient())
            {
                byte[] data = webClient.DownloadData("http://placehold.it/350x150");

                var resource = embedder.ReferenceImage(data, "image/png");

                resource.ShouldNotBeNull();
                embedder.HasImages.ShouldBeTrue();
            }
        }
Exemple #31
0
 public void Determine_content_type_from_JPG_file_extension()
 {
     var embedder = new ImageEmbedder(StubLinkedResource);
     var resource = embedder.AddImage("test.jpg");
     resource.ContentType.ShouldEqual(new ContentType("image/jpeg"));
 }
Exemple #32
0
        async Task <string> RenderView(IView view, ViewDataDictionary viewData, ActionContext actionContext, ImageEmbedder imageEmbedder)
        {
            //https://github.com/aspnet/Mvc/blob/master/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/ViewExecutor.cs
            var response = actionContext.HttpContext.Response;
            var tempDataDictionaryFactory = serviceProvider.GetRequiredService <ITempDataDictionaryFactory>();
            var viewOptions = serviceProvider.GetService <Microsoft.Extensions.Options.IOptions <MvcViewOptions> >();

            using (var writer = new StringWriter())
            {
                var tempData    = tempDataDictionaryFactory.GetTempData(actionContext.HttpContext);
                var viewContext = new ViewContext(actionContext, view, viewData, tempData, writer, viewOptions.Value.HtmlHelperOptions);
                viewData[ImageEmbedder.ViewDataKey] = imageEmbedder;
                await view.RenderAsync(viewContext);

                viewData.Remove(ImageEmbedder.ViewDataKey);
                await writer.FlushAsync();

                return(writer.GetStringBuilder().ToString());
            }
        }
Exemple #33
0
        public void ReplaceLinkedImagesWithEmbeddedImages_replaces_cid_reference()
        {
            var embedder = new ImageEmbedder();
            var resource = embedder.ReferenceImage("postal.png");

            string body = "<img src=\"cid:" + resource.ContentId + @"""/>";
            var view = AlternateView.CreateAlternateViewFromString(body);
            embedder.AddImagesToView(view);

            string replaced = TemplateViewResult.ReplaceLinkedImagesWithEmbeddedImages(view, body);
            Assert.DoesNotContain("cid:", replaced);
        }