public void MailMessage_is_correct_if_only_html_available()
        {
            // ARRANGE
            var queueMock = new Mock<IMailQueue>();
            queueMock.Setup(m => m.Enqueue(It.IsAny<Models.Mail>(), It.IsAny<object>()));

            var configMock = new Mock<IConfiguration>();

            var subject = new MailSenderService(queueMock.Object, configMock.Object);

            var renderedMail = new RenderedMail();
            renderedMail.Sender = new MailAddress("*****@*****.**");
            renderedMail.Receiver = new MailAddress("*****@*****.**");
            renderedMail.Subject = "foo";
            renderedMail.Body = "htmlbody";
            renderedMail.TextBody = null;

            // ACT
            var mailMessage = subject.CreateMailMessage(renderedMail);

            // ASSERT
            var html = mailMessage.AlternateViews.FirstOrDefault(x => x.ContentType.MediaType == MediaTypeNames.Text.Html);
            Assert.AreEqual("htmlbody", new StreamReader(html.ContentStream).ReadToEnd());

            var text = mailMessage.AlternateViews.FirstOrDefault(x => x.ContentType.MediaType == MediaTypeNames.Text.Plain);
            // html was transferred to plain text
            Assert.AreEqual("htmlbody", new StreamReader(text.ContentStream).ReadToEnd().Trim());
        }
        public bool Dequeue(Func<Models.RenderedMail, bool> processor)
        {
            var newestItem = m_mailQueue.Dequeue();

            if (newestItem != null)
            {

                var rendered = new RenderedMail();
                rendered.Receiver = newestItem.Receiver;
                rendered.Sender = newestItem.Sender;
                rendered.Subject = newestItem.Subject;
                rendered.Body = newestItem.Body;
                rendered.TextBody = newestItem.TextBody;

                return processor(rendered);
            }

            return false;
          
        }
        public bool Dequeue(Func<Models.RenderedMail, bool> processor)
        {
            return m_fileSystem.MoveOldestToDir(
                m_queueDir,
                m_workerDir,
                m_sentDir,
                m_errorDir,
                (contents) =>
                {
                    var deserialized = JsonConvert.DeserializeObject<Models.Mail>(contents, m_settings);

                    var rendered = new RenderedMail();
                    rendered.Receiver = deserialized.Receiver;
                    rendered.Sender = deserialized.Sender;
                    rendered.Subject = deserialized.Subject;
                    rendered.Body = deserialized.Body;
                    rendered.TextBody = deserialized.TextBody;
                    
                    return processor(rendered);
                });
        }
        public MailMessage CreateMailMessage(RenderedMail mail)
        {
            var mm = new MailMessage(mail.Sender, mail.Receiver)
            {
                Subject = mail.Subject,
            };
            
            // Use a defined text mail if we have one, otherwise use the converted html text
            string plainTextBody = mail.TextBody;

            if (string.IsNullOrWhiteSpace((plainTextBody ?? string.Empty).Trim()))
            {
                plainTextBody = HtmlToPlainText.ConvertHtml(mail.Body);
            }
            
            AlternateView plainTextView =
                AlternateView.CreateAlternateViewFromString(
                    plainTextBody, null, MediaTypeNames.Text.Plain);

            mm.AlternateViews.Add(plainTextView);

            mm.Body = plainTextBody;
            mm.IsBodyHtml = false;

            if (!string.IsNullOrWhiteSpace((mail.Body ?? string.Empty).Trim()))
            {
                AlternateView htmlView =
                    AlternateView.CreateAlternateViewFromString(
                        mail.Body, null, MediaTypeNames.Text.Html);

                mm.AlternateViews.Add(htmlView);

                mm.Body = mail.Body;
                mm.IsBodyHtml = true;
            }

            return mm;
        }
        public void Smtp_All_Credentials_IsOverwritten_ButNoOther_WhenSet()
        {
            // ARRANGE
            var queueMock = new Mock<IMailQueue>();
            queueMock.Setup(m => m.Enqueue(It.IsAny<Models.Mail>(), It.IsAny<object>()));

            var configMock = new Mock<IConfiguration>();
            configMock.Setup(m => m.GetValue(MailSenderService.APP_KEY_OVERRIDE_SMTP_USER))
                .Returns("a");
            configMock.Setup(m => m.GetValue(MailSenderService.APP_KEY_OVERRIDE_SMTP_PASSWORD))
                .Returns("c");

            var smtpMock = new Mock<ISmtpClient>();

            var subject = new MailSenderService(queueMock.Object, configMock.Object);

            var renderedMail = new RenderedMail();
            renderedMail.Sender = new MailAddress("*****@*****.**");
            renderedMail.Receiver = new MailAddress("*****@*****.**");
            renderedMail.Subject = "foo";
            renderedMail.Body = "htmlbody";
            renderedMail.TextBody = "textbody";

            // ACT
            var mailMessage = subject.ProcessInternal(renderedMail, smtpMock.Object);

            // ASSERT
            smtpMock.Verify(c => c.SetHost("example.org"), Times.Never);
            smtpMock.Verify(c => c.SetPort(1234), Times.Never);
            smtpMock.Verify(c => c.SetCredentials(null, "a", "c"), Times.Once);
            smtpMock.Verify(c => c.SetEnableSsl(It.IsAny<bool>()), Times.Never);
        }
        public void Recipient_is_overridden_if_set()
        {
            // ARRANGE
            var queueMock = new Mock<IMailQueue>();
            queueMock.Setup(m => m.Enqueue(It.IsAny<Models.Mail>(), It.IsAny<object>()));

            var configMock = new Mock<IConfiguration>();
            configMock.Setup(m => m.GetValue(MailSenderService.APP_KEY_OVERRIDE_RECIPIENT))
                .Returns("*****@*****.**");

            var smtpMock = new Mock<ISmtpClient>();

            var subject = new MailSenderService(queueMock.Object, configMock.Object);

            var renderedMail = new RenderedMail();
            renderedMail.Sender = new MailAddress("*****@*****.**");
            renderedMail.Receiver = new MailAddress("*****@*****.**");
            renderedMail.Subject = "foo";
            renderedMail.Body = "htmlbody";
            renderedMail.TextBody = "textbody";

            // ACT
            var mailMessage = subject.ProcessInternal(renderedMail, smtpMock.Object);


            // ASSERT
            smtpMock.Verify(c => c.Send(
                It.Is<MailMessage>(m =>
                    m.To.Count == 1
                    && m.To.First().Address == "*****@*****.**")),
                Times.Once);
        }