Beispiel #1
0
        public void Alternative_views_are_added_to_MailMessage()
        {
            var input = @"
            To: [email protected]
            From: [email protected]
            Subject: Test Subject
            Views: Text, Html";
            var text = @"Content-Type: text/plain

            Hello, World!";
            var html = @"Content-Type: text/html

            <p>Hello, World!</p>";

            var email = new Email("Test");
            var renderer = new Mock<IEmailViewRenderer>();
            renderer.Setup(r => r.Render(email, "Test.Text")).Returns(text);
            renderer.Setup(r => r.Render(email, "Test.Html")).Returns(html);

            var parser = new EmailParser(renderer.Object);
            using (var message = parser.Parse(input, email))
            {
                message.AlternateViews[0].ContentType.ShouldEqual(new ContentType("text/plain; charset=utf-16"));
                var textContent = new StreamReader(message.AlternateViews[0].ContentStream).ReadToEnd();
                textContent.ShouldEqual("Hello, World!");

                message.AlternateViews[1].ContentType.ShouldEqual(new ContentType("text/html; charset=utf-16"));
                var htmlContent = new StreamReader(message.AlternateViews[1].ContentStream).ReadToEnd();
                htmlContent.ShouldEqual("<p>Hello, World!</p>");
            }
        }
        public void Parse_creates_MailMessage_with_headers_and_body()
        {
            var input = @"
To: [email protected]
From: [email protected]
CC: [email protected]
Bcc: [email protected]
Reply-To: [email protected]
Sender: [email protected]
Priority: high
X-Test: test
Subject: Test Subject

Hello, World!";
            var renderer = new Mock<IEmailViewRenderer>();
            var parser = new EmailParser(renderer.Object);
            using (var message = parser.Parse(input, new Email("Test")))
            {
                message.To[0].Address.ShouldEqual("*****@*****.**");
                message.From.Address.ShouldEqual("*****@*****.**");
                message.CC[0].Address.ShouldEqual("*****@*****.**");
                message.Bcc[0].Address.ShouldEqual("*****@*****.**");
                message.ReplyToList[0].Address.ShouldEqual("*****@*****.**");
                message.Subject.ShouldEqual("Test Subject");
                message.Sender.Address.ShouldEqual("*****@*****.**");
                message.Priority.ShouldEqual(MailPriority.High);
                message.Headers["X-Test"].ShouldEqual("test");
                message.Body.ShouldEqual("Hello, World!");
                message.IsBodyHtml.ShouldBeFalse();
            }
        }
Beispiel #3
0
        public void Can_detect_HTML_body()
        {
            var input = @"
            To: [email protected]
            From: [email protected]
            Subject: Test Subject

            <p>Hello, World!</p>";
            var renderer = new Mock<IEmailViewRenderer>();
            var parser = new EmailParser(renderer.Object);
            using (var message = parser.Parse(input, new Email("Test")))
            {
                message.Body.ShouldEqual("<p>Hello, World!</p>");
                message.IsBodyHtml.ShouldBeTrue();
            }
        }
Beispiel #4
0
        public void Attachments_are_added_to_MailMessage()
        {
            var input = @"
            To: [email protected]
            From: [email protected]
            Subject: Test Subject

            Hello, World!";
            var email = new Email("Test");
            email.Attach(new Attachment(new MemoryStream(), "name"));
            var parser = new EmailParser(Mock.Of<IEmailViewRenderer>());

            var message = parser.Parse(input, email);

            message.Attachments.Count.ShouldEqual(1);
        }
        public void Parse_creates_email_addresses_with_display_name()
        {
            var input = @"
To: John H Smith [email protected]
From: [email protected]
Subject: Test Subject

Hello, World!";
            var renderer = new Mock<IEmailViewRenderer>();
            var parser = new EmailParser(renderer.Object);
            using (var message = parser.Parse(input, new Email("Test")))
            {
                message.To[0].Address.ShouldEqual("*****@*****.**");
                message.To[0].DisplayName.ShouldEqual("John H Smith");
            }
        }
        public void Repeating_CC_adds_each_email_address_to_list()
        {
            var input = @"
To: [email protected]
From: [email protected]
CC: [email protected]
CC: [email protected]
CC: [email protected]
Subject: Test Subject

Hello, World!";
            var renderer = new Mock<IEmailViewRenderer>();
            var parser = new EmailParser(renderer.Object);
            using (var message = parser.Parse(input, new Email("Test")))
            {
                message.CC[0].Address.ShouldEqual("*****@*****.**");
                message.CC[1].Address.ShouldEqual("*****@*****.**");
                message.CC[2].Address.ShouldEqual("*****@*****.**");                
            }
        }
Beispiel #7
0
 public void To_header_can_be_set_automatically()
 {
     dynamic email = new Email("Test");
     email.To = "*****@*****.**";
     var parser = new EmailParser(Mock.Of<IEmailViewRenderer>());
     using (var message = parser.Parse("body", (Email)email))
     {
         message.To[0].Address.ShouldEqual("*****@*****.**");
     }
 }
Beispiel #8
0
 public void Subject_header_can_be_set_automatically()
 {
     dynamic email = new Email("Test");
     email.Subject = "test";
     var parser = new EmailParser(Mock.Of<IEmailViewRenderer>());
     using (var message = parser.Parse("body", (Email)email))
     {
         message.Subject.ShouldEqual("test");
     }
 }
Beispiel #9
0
 public void From_header_can_be_set_automatically_as_MailAddress()
 {
     dynamic email = new Email("Test");
     email.From = new MailAddress("*****@*****.**");
     var parser = new EmailParser(Mock.Of<IEmailViewRenderer>());
     using (var message = parser.Parse("body", (Email)email))
     {
         message.From.ShouldEqual(new MailAddress("*****@*****.**"));
     }
 }
        public void ContentType_determined_by_view_name_when_alternative_view_is_missing_Content_Type_header()
        {
            var input = @"
To: [email protected]
From: [email protected]
Subject: Test Subject
Views: Text, Html";
            var text = @"
Hello, World!";
            var html = @"
<p>Hello, World!</p>";

            var email = new Email("Test");
            var renderer = new Mock<IEmailViewRenderer>();
            renderer.Setup(r => r.Render(email, "Test.Text")).Returns(text);
            renderer.Setup(r => r.Render(email, "Test.Html")).Returns(html);

            var parser = new EmailParser(renderer.Object);
            using (var message = parser.Parse(input, email))
            {
                message.AlternateViews[0].ContentType.MediaType.ShouldEqual("text/plain");
                message.AlternateViews[1].ContentType.MediaType.ShouldEqual("text/html");
            }
        }
Beispiel #11
0
        public void Email_address_can_include_display_name()
        {
            var input = @"To: ""John Smith"" <*****@*****.**>
            From: [email protected]
            Subject: test

            message";
            var parser = new EmailParser(Mock.Of<IEmailViewRenderer>());
            var email = new Email("Test");
            using (var message = parser.Parse(input, email))
            {
                message.To[0].Address.ShouldEqual("*****@*****.**");
                message.To[0].DisplayName.ShouldEqual("John Smith");
            }
        }
Beispiel #12
0
        public void Do_not_implicitly_add_To_from_model_when_set_in_view()
        {
            var input = @"To: [email protected]
            From: [email protected]
            Subject: test

            message";
            var parser = new EmailParser(Mock.Of<IEmailViewRenderer>());
            dynamic email = new Email("Test");
            email.To = "*****@*****.**";
            using (var message = parser.Parse(input, (Email)email))
            {
                // Check for bug reported here: http://aboutcode.net/2010/11/17/going-postal-generating-email-with-aspnet-mvc-view-engines.html#comment-153486994
                // Should not add anything extra to the 'To' list.
                message.To.Count.ShouldEqual(1);
                message.To[0].Address.ShouldEqual("*****@*****.**");
            }
        }
Beispiel #13
0
        public void Can_parse_reply_to()
        {
            var input = @"To: [email protected]
            From: [email protected]
            Reply-To: [email protected]
            Subject: test

            message";
            var parser = new EmailParser(Mock.Of<IEmailViewRenderer>());
            var email = new Email("Test");
            using (var message = parser.Parse(input, email))
            {
                message.ReplyToList[0].Address.ShouldEqual("*****@*****.**");

                // Check for bug reported here: http://aboutcode.net/2010/11/17/going-postal-generating-email-with-aspnet-mvc-view-engines.html#comment-153486994
                // Should not add anything extra to the 'To' list.
                message.To.Count.ShouldEqual(1);
                message.To[0].Address.ShouldEqual("*****@*****.**");
            }
        }
 public void Priority_header_can_be_set_automatically_from_MailPriorityEnum()
 {
     dynamic email = new Email("Test");
     email.Priority = MailPriority.High;
     var parser = new EmailParser(Mock.Of<IEmailViewRenderer>());
     using (var message = parser.Parse("body", (Email)email))
     {
         message.Priority = MailPriority.High;
     }
 }
Beispiel #15
0
        public void Can_parse_multiple_email_addresses_in_header()
        {
            var input = @"
            To: [email protected]
            From: [email protected]
            CC: [email protected], [email protected], [email protected]
            Subject: Test Subject

            Hello, World!";
            var renderer = new Mock<IEmailViewRenderer>();
            var parser = new EmailParser(renderer.Object);
            using (var message = parser.Parse(input, new Email("Test")))
            {
                message.CC[0].Address.ShouldEqual("*****@*****.**");
                message.CC[1].Address.ShouldEqual("*****@*****.**");
                message.CC[2].Address.ShouldEqual("*****@*****.**");
            }
        }
Beispiel #16
0
 /// <summary>Creates a new <see cref="EmailService"/>, using the given view engines.</summary>
 /// <param name="viewEngines">The view engines to use when creating email views.</param>
 /// <param name="createSmtpClient">A function that creates a <see cref="SmtpClient"/>. If null, a default creation function is used.</param>
 public EmailService(ViewEngineCollection viewEngines, Func<SmtpClient> createSmtpClient = null)
 {
     emailViewRenderer = new EmailViewRenderer(viewEngines);
     emailParser = new EmailParser(emailViewRenderer);
     this.createSmtpClient = createSmtpClient ?? (() => new SmtpClient());
 }
Beispiel #17
0
        public void Exception_throw_when_alternative_view_is_missing_Content_Type_header()
        {
            var input = @"
            To: [email protected]
            From: [email protected]
            Subject: Test Subject
            Views: Text, Html";
            var text = @"incorrect: header

            Hello, World!";
            var html = @"incorrect: header

            <p>Hello, World!</p>";

            var email = new Email("Test");
            var renderer = new Mock<IEmailViewRenderer>();
            renderer.Setup(r => r.Render(email, "Test.Text")).Returns(text);
            renderer.Setup(r => r.Render(email, "Test.Html")).Returns(html);

            var parser = new EmailParser(renderer.Object);
            Assert.Throws<Exception>(delegate
            {
                parser.Parse(input, email);
            });
        }