public void CanCreateEmailMessageWithRazorTemplate()
        {
            var razorMailMessageFactory = new RazorMailMessageFactory();

            var mailMessage = razorMailMessageFactory.Create
            (
                "TestTemplates.IntegrationTest.TestTemplate.cshtml",
                new { Name = "Robin" }
            );

            const string expectedResult = @"
                Start layout
                <p>This is the header</p>
                <b>This is a test</b>
                <p>This is the footer</p>
                End layout
            ";

            const string expectedPlainTextResult = @"
                Start layout
                This is the header
                This is a test
                This is the footer
                End layout
            ";

            Assert.AreEqual(1, mailMessage.AlternateViews.Count);
            Assert.AreEqual(expectedResult.StripWhiteSpace(), new StreamReader(mailMessage.AlternateViews[0].ContentStream).ReadToEnd().StripWhiteSpace());
            Assert.AreEqual(expectedPlainTextResult.StripWhiteSpace(), mailMessage.Body.StripWhiteSpace());
        }
Ejemplo n.º 2
0
        public void CanCreateCustomTemplateBaseClass()
        {
            const string template = "@Revert(\"12345\")";

            var templateResolverMock = new Mock <ITemplateResolver>();

            // Request for html template
            templateResolverMock
            .Setup(x => x.ResolveTemplate(It.IsAny <string>(), false))
            .Returns(template);

            // Request for plain text template returns empty string (indicating the plain text template should no be used)
            templateResolverMock
            .Setup(x => x.ResolveTemplate(It.IsAny <string>(), true))
            .Returns("");

            var razorMailMessageFactory = new RazorMailMessageFactory(templateResolverMock.Object, typeof(CustomTemplateBase <>), null, new Mock <InMemoryTemplateCache>().Object);

            var mailMessage = razorMailMessageFactory.Create("TestTemplate", new {});

            const string expectedResult = "54321";

            Assert.AreEqual(expectedResult, mailMessage.Body);
            Assert.AreEqual(expectedResult, new StreamReader(mailMessage.AlternateViews[0].ContentStream).ReadToEnd());
        }
Ejemplo n.º 3
0
        public void CanCreateEmailMessageWithRazorTemplate()
        {
            var razorMailMessageFactory = new RazorMailMessageFactory();

            var mailMessage = razorMailMessageFactory.Create
                              (
                "TestTemplates.IntegrationTest.TestTemplate.cshtml",
                new { Name = "Robin" }
                              );

            const string expectedResult = @"
                Start layout 
                <p>This is the header</p>
                <b>This is a test</b>
                <p>This is the footer</p>
                End layout
            ";

            const string expectedPlainTextResult = @"
                Start layout 
                This is the header
                This is a test
                This is the footer
                End layout
            ";

            Assert.AreEqual(1, mailMessage.AlternateViews.Count);
            Assert.AreEqual(expectedResult.StripWhiteSpace(), new StreamReader(mailMessage.AlternateViews[0].ContentStream).ReadToEnd().StripWhiteSpace());
            Assert.AreEqual(expectedPlainTextResult.StripWhiteSpace(), mailMessage.Body.StripWhiteSpace());
        }
        public void CanAddViewBag()
        {
            var templateResolverMock = new Mock<ITemplateResolver>();

            // Request for html template
            templateResolverMock
                .Setup(x => x.ResolveTemplate(It.IsAny<string>(), false))
                .Returns("<b>Welcome @ViewBag.Name</b>");

            // Request for plain text template returns empty string (indicating the plain text template should no be used)
            templateResolverMock
                .Setup(x => x.ResolveTemplate(It.IsAny<string>(), true))
                .Returns("");

            var razorMailMessageFactory = new RazorMailMessageFactory(templateResolverMock.Object, typeof(DefaultTemplateBase<>), null, new Mock<InMemoryTemplateCache>().Object);
            var model = new { };

            dynamic viewBag = new DynamicViewBag();
            viewBag.Name = "Robin";

            var mailMessage = razorMailMessageFactory.Create("TestTemplate", model, viewBag);

            const string expectedResult = "<b>Welcome Robin</b>";
            Assert.AreEqual(expectedResult, mailMessage.Body);
            Assert.AreEqual(expectedResult, new StreamReader(mailMessage.AlternateViews[0].ContentStream).ReadToEnd());
        }
Ejemplo n.º 5
0
        private static void SendMailMessageWithEmbeddedImage()
        {
            // Use namespace: MailTemplates.SendMailMessageWithEmbeddedImage
            var razorMailMessageFactory = new RazorMailMessageFactory
                                          (
                new DefaultTemplateResolver("MailTemplates")
                                          );

            var mailMessage = razorMailMessageFactory.Create
                              (
                "SendMailMessageWithEmbeddedImage.TestTemplate.cshtml",
                new { Name = "Robin" },
                new List <LinkedResource> {
                new LinkedResource("MailTemplates/SendMailMessageWithEmbeddedImage/chuck_mailheader.png")
                {
                    ContentId = "chuckNorrisImage"
                }
            }
                              );

            mailMessage.From = new MailAddress(FromEmailAddress);
            mailMessage.To.Add(new MailAddress(ToEmailAddress));
            mailMessage.Subject = "Test template";

            SmtpClient.Send(mailMessage);
        }
Ejemplo n.º 6
0
        public void CanCreateMultipartMailMessage()
        {
            var templateResolverMock = new Mock <ITemplateResolver>();

            // Request for html template
            templateResolverMock
            .Setup(x => x.ResolveTemplate(It.IsAny <string>(), false))
            .Returns("<b>Welcome @Model.Name</b>");

            // Setup plain text template request
            templateResolverMock
            .Setup(x => x.ResolveTemplate(It.IsAny <string>(), true))
            .Returns("Welcome @Model.Name");

            var razorMailMessageFactory = new RazorMailMessageFactory(templateResolverMock.Object, typeof(DefaultTemplateBase <>), null, new Mock <InMemoryTemplateCache>().Object);
            var model = new { Name = "Robin" };

            var mailMessage = razorMailMessageFactory.Create("TestTemplate", model);

            const string expectedResult          = "<b>Welcome Robin</b>";
            const string expectedPlainTextResult = "Welcome Robin";

            Assert.AreEqual(1, mailMessage.AlternateViews.Count);
            Assert.AreEqual(expectedResult, new StreamReader(mailMessage.AlternateViews[0].ContentStream).ReadToEnd());
            Assert.AreEqual(expectedPlainTextResult, mailMessage.Body);
        }
Ejemplo n.º 7
0
        public void CanCreateMailMessageWithLayout()
        {
            var templateResolverMock = new Mock <ITemplateResolver>();

            // Request for layout
            templateResolverMock
            .Setup(x => x.ResolveLayout("TestLayout"))
            .Returns("Start Layout @RenderBody() End Layout");

            // Request for html template
            templateResolverMock
            .Setup(x => x.ResolveTemplate("TestTemplate", false))
            .Returns("@{ Layout = \"TestLayout\";}<b>Welcome @Model.Name</b>");

            // Request for plain text template returns empty string (indicating the plain text template should no be used)
            templateResolverMock
            .Setup(x => x.ResolveTemplate("TestTemplate", true))
            .Returns("");

            var razorMailMessageFactory = new RazorMailMessageFactory(templateResolverMock.Object, typeof(DefaultTemplateBase <>), null, new Mock <InMemoryTemplateCache>().Object);
            var model = new { Name = "Robin" };

            var mailMessage = razorMailMessageFactory.Create("TestTemplate", model);

            const string expectedResult = "Start Layout <b>Welcome Robin</b> End Layout";

            Assert.AreEqual(expectedResult, mailMessage.Body);
            Assert.AreEqual(expectedResult, new StreamReader(mailMessage.AlternateViews[0].ContentStream).ReadToEnd());
        }
Ejemplo n.º 8
0
        public void TemplatesAreOnlyResolvedWhenNotCached()
        {
            const string layoutTemplate = "Start Layout @RenderBody() End Layout";
            const string htmlTemplate   = "@{ Layout = \"TestLayout\";}<b>Welcome @Model.Name</b>";
            const string textTemplate   = "@{ Layout = \"TestLayout\";}Welcome @Model.Name";

            var templateCacheMock = new Mock <ITemplateCache>();

            templateCacheMock.Setup(x => x.Get("TestTemplate")).Returns(htmlTemplate);
            templateCacheMock.Setup(x => x.Get("TestTemplate.text")).Returns(textTemplate);
            templateCacheMock.Setup(x => x.Get("TestLayout")).Returns(layoutTemplate);

            var templateResolverMock = new Mock <ITemplateResolver>();

            var razorMailMessageFactory = new RazorMailMessageFactory(templateResolverMock.Object, typeof(DefaultTemplateBase <>), null, templateCacheMock.Object);
            var model = new { Name = "Robin" };

            var mailMessage = razorMailMessageFactory.Create("TestTemplate", model);

            const string expectedResult          = "Start Layout <b>Welcome Robin</b> End Layout";
            const string expectedPlainTextResult = "Start Layout Welcome Robin End Layout";

            Assert.AreEqual(1, mailMessage.AlternateViews.Count);
            Assert.AreEqual(expectedResult, new StreamReader(mailMessage.AlternateViews[0].ContentStream).ReadToEnd());
            Assert.AreEqual(expectedPlainTextResult, mailMessage.Body);

            templateResolverMock.Verify(x => x.ResolveTemplate("TestTemplate", false), Times.Never());
            templateResolverMock.Verify(x => x.ResolveTemplate("TestTemplate", true), Times.Never());
            templateResolverMock.Verify(x => x.ResolveLayout("TestLayout"), Times.Never());
        }
Ejemplo n.º 9
0
        public void CanAddLinkedResourcesToEmail()
        {
            var templateResolverMock = new Mock <ITemplateResolver>();

            // Request for html template
            templateResolverMock
            .Setup(x => x.ResolveTemplate(It.IsAny <string>(), false))
            .Returns("<b>Welcome @Model.Name</b>");

            // Request for plain text template returns empty string (indicating the plain text template should not be used)
            templateResolverMock
            .Setup(x => x.ResolveTemplate(It.IsAny <string>(), true))
            .Returns("");

            var razorMailMessageFactory = new RazorMailMessageFactory(templateResolverMock.Object, typeof(DefaultTemplateBase <>), null, new Mock <InMemoryTemplateCache>().Object);
            var model = new { Name = "Robin" };

            var linkedResources = new List <LinkedResource>
            {
                new LinkedResource(new MemoryStream(Encoding.ASCII.GetBytes("resource1")))
                {
                    ContentId = "resource1"
                },
                new LinkedResource(new MemoryStream(Encoding.ASCII.GetBytes("resource2")))
                {
                    ContentId = "resource2"
                }
            };

            var mailMessage = razorMailMessageFactory.Create("TestTemplate", model, linkedResources);

            Assert.AreEqual(2, mailMessage.AlternateViews[0].LinkedResources.Count);
            Assert.AreEqual("resource1", new StreamReader(mailMessage.AlternateViews[0].LinkedResources[0].ContentStream).ReadToEnd());
            Assert.AreEqual("resource2", new StreamReader(mailMessage.AlternateViews[0].LinkedResources[1].ContentStream).ReadToEnd());
        }
Ejemplo n.º 10
0
        public void CanAddViewBag()
        {
            var templateResolverMock = new Mock <ITemplateResolver>();

            // Request for html template
            templateResolverMock
            .Setup(x => x.ResolveTemplate(It.IsAny <string>(), false))
            .Returns("<b>Welcome @ViewBag.Name</b>");

            // Request for plain text template returns empty string (indicating the plain text template should no be used)
            templateResolverMock
            .Setup(x => x.ResolveTemplate(It.IsAny <string>(), true))
            .Returns("");

            var razorMailMessageFactory = new RazorMailMessageFactory(templateResolverMock.Object, typeof(DefaultTemplateBase <>), null, new Mock <InMemoryTemplateCache>().Object);
            var model = new { };

            dynamic viewBag = new DynamicViewBag();

            viewBag.Name = "Robin";

            var mailMessage = razorMailMessageFactory.Create("TestTemplate", model, viewBag);

            const string expectedResult = "<b>Welcome Robin</b>";

            Assert.AreEqual(expectedResult, mailMessage.Body);
            Assert.AreEqual(expectedResult, new StreamReader(mailMessage.AlternateViews[0].ContentStream).ReadToEnd());
        }
        public void CanAddLinkedResourcesToEmail()
        {
            var templateResolverMock = new Mock<ITemplateResolver>();

            // Request for html template
            templateResolverMock
                .Setup(x => x.ResolveTemplate(It.IsAny<string>(), false))
                .Returns("<b>Welcome @Model.Name</b>");

            // Request for plain text template returns empty string (indicating the plain text template should not be used)
            templateResolverMock
                .Setup(x => x.ResolveTemplate(It.IsAny<string>(), true))
                .Returns("");

            var razorMailMessageFactory = new RazorMailMessageFactory(templateResolverMock.Object, typeof(DefaultTemplateBase<>), null, new Mock<InMemoryTemplateCache>().Object);
            var model = new { Name = "Robin" };

            var linkedResources = new List<LinkedResource>
                {
                    new LinkedResource(new MemoryStream(Encoding.ASCII.GetBytes("resource1"))) { ContentId = "resource1"},
                    new LinkedResource(new MemoryStream(Encoding.ASCII.GetBytes("resource2"))) { ContentId = "resource2"}
                };

            var mailMessage = razorMailMessageFactory.Create("TestTemplate", model, linkedResources);

            Assert.AreEqual(2, mailMessage.AlternateViews[0].LinkedResources.Count);
            Assert.AreEqual("resource1", new StreamReader(mailMessage.AlternateViews[0].LinkedResources[0].ContentStream).ReadToEnd());
            Assert.AreEqual("resource2", new StreamReader(mailMessage.AlternateViews[0].LinkedResources[1].ContentStream).ReadToEnd());
        }
Ejemplo n.º 12
0
        private static void SendMailMessageWithDefaultSettings()
        {
            var razorMailMessageFactory = new RazorMailMessageFactory();

            var mailMessage = razorMailMessageFactory.Create("MailTemplates.SendMailMessageWithDefaultSettings.TestTemplate.cshtml", new { Name = "Robin" });

            mailMessage.From = new MailAddress(FromEmailAddress);
            mailMessage.To.Add(new MailAddress(ToEmailAddress));
            mailMessage.Subject = "Test template";

            SmtpClient.Send(mailMessage);
        }
Ejemplo n.º 13
0
        private static void SendMailMessageWithDefaultSettings()
        {
            var razorMailMessageFactory = new RazorMailMessageFactory();

            var mailMessage = razorMailMessageFactory.Create("MailTemplates.SendMailMessageWithDefaultSettings.TestTemplate.cshtml", new { Name = "Robin" });

            mailMessage.From = new MailAddress(FromEmailAddress);
            mailMessage.To.Add(new MailAddress(ToEmailAddress));
            mailMessage.Subject = "Test template";

            SmtpClient.Send(mailMessage);
        }
Ejemplo n.º 14
0
        private static void SendMailMessageWithSpecificAssemblyAndNameSpace()
        {
            var razorMailMessageFactory = new RazorMailMessageFactory
                                          (
                new DefaultTemplateResolver(Assembly.GetExecutingAssembly(), "MailTemplates")
                                          );

            var mailMessage = razorMailMessageFactory.Create("SendMailMessageWithSpecificAssemblyAndNameSpace.TestTemplate.cshtml", new { Name = "Robin" });

            mailMessage.From = new MailAddress(FromEmailAddress);
            mailMessage.To.Add(new MailAddress(ToEmailAddress));
            mailMessage.Subject = "Test template";

            SmtpClient.Send(mailMessage);
        }
Ejemplo n.º 15
0
        private static void SendMailMessageWithLayoutAndSections()
        {
            // Use namespace: MailTemplates.SendMailMessageWithEmbeddedImage
            var razorMailMessageFactory = new RazorMailMessageFactory
                                          (
                new DefaultTemplateResolver("MailTemplates")
                                          );

            var mailMessage = razorMailMessageFactory.Create
                              (
                "SendMailMessageWithLayoutAndSections.TestTemplate.cshtml",
                new { Name = "Robin" }
                              );

            mailMessage.From = new MailAddress(FromEmailAddress);
            mailMessage.To.Add(new MailAddress(ToEmailAddress));
            mailMessage.Subject = "Test template";

            SmtpClient.Send(mailMessage);
        }
Ejemplo n.º 16
0
        private static void SendMailMessageWithLayoutAndSections()
        {
            // Use namespace: MailTemplates.SendMailMessageWithEmbeddedImage
            var razorMailMessageFactory = new RazorMailMessageFactory
            (
                new DefaultTemplateResolver("MailTemplates")
            );

            var mailMessage = razorMailMessageFactory.Create
            (
                "SendMailMessageWithLayoutAndSections.TestTemplate.cshtml",
                new { Name = "Robin" }
            );

            mailMessage.From = new MailAddress(FromEmailAddress);
            mailMessage.To.Add(new MailAddress(ToEmailAddress));
            mailMessage.Subject = "Test template";

            SmtpClient.Send(mailMessage);
        }
Ejemplo n.º 17
0
        private static void SendMailMessageWithSpecificAssemblyAndNameSpace()
        {
            var razorMailMessageFactory = new RazorMailMessageFactory
            (
                new DefaultTemplateResolver(Assembly.GetExecutingAssembly(), "MailTemplates")
            );

            var mailMessage = razorMailMessageFactory.Create("SendMailMessageWithSpecificAssemblyAndNameSpace.TestTemplate.cshtml", new { Name = "Robin" });

            mailMessage.From = new MailAddress(FromEmailAddress);
            mailMessage.To.Add(new MailAddress(ToEmailAddress));
            mailMessage.Subject = "Test template";

            SmtpClient.Send(mailMessage);
        }
Ejemplo n.º 18
0
        private static void SendMailMessageWithEmbeddedImage()
        {
            // Use namespace: MailTemplates.SendMailMessageWithEmbeddedImage
            var razorMailMessageFactory = new RazorMailMessageFactory
            (
                new DefaultTemplateResolver("MailTemplates")
            );

            var mailMessage = razorMailMessageFactory.Create
            (
                "SendMailMessageWithEmbeddedImage.TestTemplate.cshtml",
                new { Name = "Robin" },
                new List<LinkedResource> { new LinkedResource("MailTemplates/SendMailMessageWithEmbeddedImage/chuck_mailheader.png") { ContentId = "chuckNorrisImage" } }
            );

            mailMessage.From = new MailAddress(FromEmailAddress);
            mailMessage.To.Add(new MailAddress(ToEmailAddress));
            mailMessage.Subject = "Test template";

            SmtpClient.Send(mailMessage);
        }
        public void TemplatesAreOnlyResolvedWhenNotCached()
        {
            const string layoutTemplate = "Start Layout @RenderBody() End Layout";
            const string htmlTemplate = "@{ Layout = \"TestLayout\";}<b>Welcome @Model.Name</b>";
            const string textTemplate = "@{ Layout = \"TestLayout\";}Welcome @Model.Name";

            var templateCacheMock = new Mock<ITemplateCache>();

            templateCacheMock.Setup(x => x.Get("TestTemplate")).Returns(htmlTemplate);
            templateCacheMock.Setup(x => x.Get("TestTemplate.text")).Returns(textTemplate);
            templateCacheMock.Setup(x => x.Get("TestLayout")).Returns(layoutTemplate);

            var templateResolverMock = new Mock<ITemplateResolver>();

            var razorMailMessageFactory = new RazorMailMessageFactory(templateResolverMock.Object, typeof(DefaultTemplateBase<>), null, templateCacheMock.Object);
            var model = new { Name = "Robin" };

            var mailMessage = razorMailMessageFactory.Create("TestTemplate", model);

            const string expectedResult = "Start Layout <b>Welcome Robin</b> End Layout";
            const string expectedPlainTextResult = "Start Layout Welcome Robin End Layout";

            Assert.AreEqual(1, mailMessage.AlternateViews.Count);
            Assert.AreEqual(expectedResult, new StreamReader(mailMessage.AlternateViews[0].ContentStream).ReadToEnd());
            Assert.AreEqual(expectedPlainTextResult, mailMessage.Body);

            templateResolverMock.Verify(x => x.ResolveTemplate("TestTemplate", false), Times.Never());
            templateResolverMock.Verify(x => x.ResolveTemplate("TestTemplate", true), Times.Never());
            templateResolverMock.Verify(x => x.ResolveLayout("TestLayout"), Times.Never());
        }
        public void CanCreateMultipartMailMessageWithLayout()
        {
            var templateResolverMock = new Mock<ITemplateResolver>();

            // Request for layout
            templateResolverMock
                .Setup(x => x.ResolveLayout("TestLayout"))
                .Returns("Start Layout @RenderBody() End Layout");

            // Request for html template
            templateResolverMock
                .Setup(x => x.ResolveTemplate("TestTemplate", false))
                .Returns("@{ Layout = \"TestLayout\";}<b>Welcome @Model.Name</b>");

            // Request for plain text template
            templateResolverMock
                .Setup(x => x.ResolveTemplate("TestTemplate", true))
                .Returns("@{ Layout = \"TestLayout\";}Welcome @Model.Name");

            var razorMailMessageFactory = new RazorMailMessageFactory(templateResolverMock.Object, typeof(DefaultTemplateBase<>), null, new Mock<InMemoryTemplateCache>().Object);
            var model = new { Name = "Robin" };

            var mailMessage = razorMailMessageFactory.Create("TestTemplate", model);

            const string expectedResult = "Start Layout <b>Welcome Robin</b> End Layout";
            const string expectedPlainTextResult = "Start Layout Welcome Robin End Layout";

            Assert.AreEqual(1, mailMessage.AlternateViews.Count);
            Assert.AreEqual(expectedResult, new StreamReader(mailMessage.AlternateViews[0].ContentStream).ReadToEnd());
            Assert.AreEqual(expectedPlainTextResult, mailMessage.Body);
        }
        public void CanCreateMultipartMailMessage()
        {
            var templateResolverMock = new Mock<ITemplateResolver>();

            // Request for html template
            templateResolverMock
                .Setup(x => x.ResolveTemplate(It.IsAny<string>(), false))
                .Returns("<b>Welcome @Model.Name</b>");

            // Setup plain text template request
            templateResolverMock
                    .Setup(x => x.ResolveTemplate(It.IsAny<string>(), true))
                    .Returns("Welcome @Model.Name");

            var razorMailMessageFactory = new RazorMailMessageFactory(templateResolverMock.Object, typeof(DefaultTemplateBase<>), null, new Mock<InMemoryTemplateCache>().Object);
            var model = new { Name = "Robin" };

            var mailMessage = razorMailMessageFactory.Create("TestTemplate", model);

            const string expectedResult = "<b>Welcome Robin</b>";
            const string expectedPlainTextResult = "Welcome Robin";

            Assert.AreEqual(1, mailMessage.AlternateViews.Count);
            Assert.AreEqual(expectedResult, new StreamReader(mailMessage.AlternateViews[0].ContentStream).ReadToEnd());
            Assert.AreEqual(expectedPlainTextResult, mailMessage.Body);
        }