예제 #1
0
        public async Task Test_SendEmail_ByTemplate_WithDictionnary()
        {
            _smtpServer.ClearReceivedEmail();

            var emailTemplateProviderMoq = new Mock <IEmailTemplateProvider>();

            emailTemplateProviderMoq.Setup(e => e.GetTemplate(It.IsAny <string>())).ReturnsAsync(new EmailTemplate("Duy")
            {
                ToEmails = "[DuyEmail],{HBDEmail},[email protected]",
                Body     = "Hello [Name]",
            });

            using (var mailService = new SmtpEmailService(
                       new MailMessageProvider(emailTemplateProviderMoq.Object, new TransformerService()), new SmtpEmailOptions
            {
                FromEmailAddress = new MailAddress("*****@*****.**"),
                SmtpClientFactory = () => new SmtpClient("localhost", 25)
            }))
            {
                await mailService.SendAsync("Duy", new object[]
                {
                    new Dictionary <string, object>
                    {
                        ["DuyEmail"] = "*****@*****.**",
                        ["HBDEmail"] = "*****@*****.**",
                        ["Name"]     = "Duy Hoang"
                    }
                });
            }

            _smtpServer.ReceivedEmailCount.Should().BeGreaterOrEqualTo(1);
        }
예제 #2
0
        public void CanSendEmailAsync()
        {
            _client.Setup(x => x.SendMailAsync(It.IsAny <MailMessage>(), It.IsAny <CancellationToken>())).Returns(Task.CompletedTask);
            _clientFactory.Setup(x => x.CreateInstance()).Returns(_client.Object);

            var smtpService = new SmtpEmailService(new LoggingService(), _clientFactory.Object);
            var result      = smtpService.SendAsync(new MailAddress("*****@*****.**"), new MailAddress("*****@*****.**"), "subject", "content", CancellationToken.None).Result;

            Assert.True(result);
        }
예제 #3
0
        public void ThrowsOnNullRecipientEmailAddress()
        {
            _client.Setup(x => x.SendMailAsync(It.IsAny <MailMessage>(), It.IsAny <CancellationToken>())).Returns(Task.CompletedTask);
            _clientFactory.Setup(x => x.CreateInstance()).Returns(_client.Object);

            var smtpService = new SmtpEmailService(new LoggingService(), _clientFactory.Object);

            Assert.ThrowsAsync <ArgumentNullException>(async() => {
                await smtpService.SendAsync(new MailAddress("*****@*****.**"), null, "subject", "content", CancellationToken.None);
            });
        }
예제 #4
0
        public void ThrowsSmtpExceptionOnFailedSend()
        {
            _client.Setup(x => x.SendMailAsync(It.IsAny <MailMessage>(), It.IsAny <CancellationToken>())).Throws <SmtpException>();
            _clientFactory.Setup(x => x.CreateInstance()).Returns(_client.Object);

            var smtpService = new SmtpEmailService(new LoggingService(), _clientFactory.Object);

            Assert.ThrowsAsync <SmtpException>(async() =>
            {
                await smtpService.SendAsync(new MailAddress("*****@*****.**"), new MailAddress("*****@*****.**"), "subject", "content", CancellationToken.None);
            });
        }