コード例 #1
0
        public void CoordinatorCreatedNoEmailWithDefaultEmailForCoordinatorSendEmail()
        {
            var ravenDocStore = MockRepository.GenerateMock<IRavenDocStore>();
            var session = MockRepository.GenerateMock<IDocumentSession>();
            var mailActioner = MockRepository.GenerateMock<IMailActioner>();
            var dateTimeMapper = MockRepository.GenerateMock<IDateTimeOlsenFromUtcMapping>();

            ravenDocStore.Expect(r => r.GetStore().OpenSession("Configuration"))
                .Return(session);
            var emailDefaultNotification = new EmailDefaultNotification { EmailAddresses = new List<string> { "*****@*****.**", "*****@*****.**" } };
            session.Expect(s => s.Load<EmailDefaultNotification>("EmailDefaultConfig")).Return(emailDefaultNotification);
            var mailgunConfig = new MailgunConfiguration {ApiKey = "key", DomainName = "domain", DefaultFrom = "*****@*****.**"};
            session.Expect(s => s.Load<MailgunConfiguration>("MailgunConfig")).Return(mailgunConfig);
            var message = new MailMessage();
            mailActioner.Expect(m => m.Send(Arg<MailgunConfiguration>.Is.Equal(mailgunConfig), Arg<MailMessage>.Is.NotNull)).WhenCalled(a => message = (MailMessage)(a.Arguments[1]));
            dateTimeMapper.Expect(d => d.DateTimeUtcToLocalWithOlsenZone(Arg<DateTime>.Is.Anything, Arg<string>.Is.Anything)).Return(DateTime.Now).Repeat.Any();

            var emailService = new EmailService { RavenDocStore = ravenDocStore, MailActioner = mailActioner, DateTimeOlsenFromUtcMapping = dateTimeMapper };
            var coordinatorComplete = new CoordinatorCreated { ScheduledMessages = new List<MessageSchedule> { new MessageSchedule { ScheduledTimeUtc = DateTime.Now }}, MetaData = new SmsMetaData()};
            emailService.Handle(new CoordinatorCreatedEmail(coordinatorComplete));

            Assert.That(message.From.ToString(), Is.EqualTo(mailgunConfig.DefaultFrom));
            Assert.That(message.To[0].Address, Is.EqualTo(emailDefaultNotification.EmailAddresses[0]));
            Assert.That(message.To[1].Address, Is.EqualTo(emailDefaultNotification.EmailAddresses[1]));
        }
コード例 #2
0
        public PartialViewResult EditAjax(MailgunConfiguration configuration)
        {
            var isValid = TryUpdateModel(configuration);

            if (!isValid)
            {
                return(PartialView("_MailgunConfigEdit", configuration));
            }
            using (var session = DocumentStore.GetStore().OpenSession("Configuration"))
            {
                var mailgunConfiguration = session.Load <MailgunConfiguration>("MailgunConfig");
                if (mailgunConfiguration == null)
                {
                    session.Store(configuration, "MailgunConfig");
                }
                else
                {
                    mailgunConfiguration.ApiKey      = configuration.ApiKey;
                    mailgunConfiguration.DefaultFrom = configuration.DefaultFrom;
                    mailgunConfiguration.DomainName  = configuration.DomainName;
                }
                session.SaveChanges();
                return(PartialView("_MailgunConfigDetails", configuration));
            }
        }
コード例 #3
0
        public void CoordinatorCompleteWithEmailDefaultFromNotSentThrowsException()
        {
            var ravenDocStore = MockRepository.GenerateMock <IRavenDocStore>();
            var session       = MockRepository.GenerateMock <IDocumentSession>();

            ravenDocStore.Expect(r => r.GetStore().OpenSession("Configuration"))
            .Return(session);
            var emailDefaultNotification = new EmailDefaultNotification {
                EmailAddresses = new List <string> {
                    "*****@*****.**", "*****@*****.**"
                }
            };

            session.Expect(s => s.Load <EmailDefaultNotification>("EmailDefaultConfig")).Return(emailDefaultNotification);
            var mailgunConfig = new MailgunConfiguration {
                ApiKey = "key", DomainName = "domain", DefaultFrom = string.Empty
            };

            session.Expect(s => s.Load <MailgunConfiguration>("MailgunConfig")).Return(mailgunConfig);

            var emailService = new EmailService {
                RavenDocStore = ravenDocStore
            };
            var coordinatorComplete = new CoordinatorCompleteEmailWithSummary {
                EmailAddresses = new List <string> {
                    "*****@*****.**"
                }
            };

            Assert.That(() => emailService.Handle(coordinatorComplete), Throws.Exception.With.Message.EqualTo("Could not find the default 'From' sender."));
        }
コード例 #4
0
        public void CoordinatorCompleteWithEmailAndDefaultEmailSendsEmail()
        {
            var mailActioner   = MockRepository.GenerateMock <IMailActioner>();
            var ravenDocStore  = MockRepository.GenerateMock <IRavenDocStore>();
            var dateTimeMapper = MockRepository.GenerateMock <IDateTimeOlsenFromUtcMapping>();
            var session        = MockRepository.GenerateMock <IDocumentSession>();

            ravenDocStore.Expect(r => r.GetStore().OpenSession("Configuration"))
            .Return(session);
            var mailgunConfig = new MailgunConfiguration {
                ApiKey = "key", DomainName = "domain", DefaultFrom = "*****@*****.**"
            };

            session.Expect(s => s.Load <MailgunConfiguration>("MailgunConfig")).Return(mailgunConfig);
            var emailDefaultNotification = new EmailDefaultNotification {
                EmailAddresses = new List <string> {
                    "*****@*****.**", "*****@*****.**"
                }
            };

            session.Expect(s => s.Load <EmailDefaultNotification>("EmailDefaultConfig")).Return(emailDefaultNotification);

            var message = new MailMessage();

            mailActioner.Expect(m => m.Send(Arg <MailgunConfiguration> .Is.Equal(mailgunConfig), Arg <MailMessage> .Is.NotNull)).WhenCalled(a => message = (MailMessage)(a.Arguments[1]));
            dateTimeMapper.Expect(d => d.DateTimeUtcToLocalWithOlsenZone(Arg <DateTime> .Is.Anything, Arg <string> .Is.Anything)).Return(DateTime.Now).Repeat.Any();

            var emailService = new EmailService {
                MailActioner = mailActioner, RavenDocStore = ravenDocStore, DateTimeOlsenFromUtcMapping = dateTimeMapper
            };
            var coordinatorComplete = new CoordinatorCompleteEmailWithSummary
            {
                CoordinatorId  = Guid.NewGuid(),
                EmailAddresses = new List <string> {
                    "*****@*****.**", "*****@*****.**"
                },
                FinishTimeUtc = DateTime.Now,
                StartTimeUtc  = DateTime.Now.AddMinutes(-10),
                FailedCount   = 4,
                SuccessCount  = 3,
                Cost          = 33.44m
            };

            emailService.Handle(coordinatorComplete);

            mailActioner.VerifyAllExpectations();
            Assert.That(message.From.ToString(), Is.EqualTo(mailgunConfig.DefaultFrom));
            Assert.That(message.To[0].Address, Is.EqualTo(coordinatorComplete.EmailAddresses[0]));
            Assert.That(message.To[1].Address, Is.EqualTo(coordinatorComplete.EmailAddresses[1]));
            Assert.That(message.To[2].Address, Is.EqualTo(emailDefaultNotification.EmailAddresses[0]));
            Assert.That(message.To[3].Address, Is.EqualTo(emailDefaultNotification.EmailAddresses[1]));
        }
コード例 #5
0
        public void CoordinatorCreatedWithEmailWithDefaultEmailForCoordinatorSendEmail()
        {
            var ravenDocStore  = MockRepository.GenerateMock <IRavenDocStore>();
            var session        = MockRepository.GenerateMock <IDocumentSession>();
            var mailActioner   = MockRepository.GenerateMock <IMailActioner>();
            var dateTimeMapper = MockRepository.GenerateMock <IDateTimeOlsenFromUtcMapping>();

            ravenDocStore.Expect(r => r.GetStore().OpenSession("Configuration"))
            .Return(session);
            var emailDefaultNotification = new EmailDefaultNotification {
                EmailAddresses = new List <string> {
                    "*****@*****.**", "*****@*****.**"
                }
            };

            session.Expect(s => s.Load <EmailDefaultNotification>("EmailDefaultConfig")).Return(emailDefaultNotification);
            var mailgunConfig = new MailgunConfiguration {
                ApiKey = "key", DomainName = "domain", DefaultFrom = "*****@*****.**"
            };

            session.Expect(s => s.Load <MailgunConfiguration>("MailgunConfig")).Return(mailgunConfig);
            var message = new MailMessage();

            mailActioner.Expect(m => m.Send(Arg <MailgunConfiguration> .Is.Equal(mailgunConfig), Arg <MailMessage> .Is.NotNull)).WhenCalled(a => message = (MailMessage)(a.Arguments[1]));
            dateTimeMapper.Expect(d => d.DateTimeUtcToLocalWithOlsenZone(Arg <DateTime> .Is.Anything, Arg <string> .Is.Anything)).Return(DateTime.Now).Repeat.Any();

            var emailService = new EmailService {
                RavenDocStore = ravenDocStore, MailActioner = mailActioner, DateTimeOlsenFromUtcMapping = dateTimeMapper
            };
            var coordinatorComplete = new CoordinatorCreated {
                ConfirmationEmailAddresses = new List <string> {
                    "*****@*****.**"
                }, ScheduledMessages = new List <MessageSchedule> {
                    new MessageSchedule {
                        ScheduledTimeUtc = DateTime.Now
                    }
                }, MetaData = new SmsMetaData()
            };

            emailService.Handle(new CoordinatorCreatedEmail(coordinatorComplete));

            Assert.That(message.From.ToString(), Is.EqualTo(mailgunConfig.DefaultFrom));
            Assert.That(message.To[0].Address, Is.EqualTo(coordinatorComplete.ConfirmationEmailAddresses[0]));
            Assert.That(message.To[1].Address, Is.EqualTo(emailDefaultNotification.EmailAddresses[0]));
            Assert.That(message.To[2].Address, Is.EqualTo(emailDefaultNotification.EmailAddresses[1]));
        }
コード例 #6
0
 public void Send(MailgunConfiguration configuration, MailMessage message)
 {
     var client = new RestClient
     {
         BaseUrl = new Uri("https://api.mailgun.net/v2"),
         Authenticator = new HttpBasicAuthenticator("api", configuration.ApiKey)
     };
     RestRequest request = new RestRequest();
     request.AddParameter("domain", configuration.DomainName, ParameterType.UrlSegment);
     request.Resource = "{domain}/messages";
     request.AddParameter("from", message.From.DisplayName + " <" + message.From.Address + ">");
     request.AddParameter("to", string.Join(",", message.To.Select(t => t.Address).ToArray()));
     request.AddParameter("subject", message.Subject);
     //            request.AddParameter("text", message.Body);
     request.AddParameter("html", message.Body);
     request.AddParameter("h:Reply-To", message.ReplyToList.Select(r => r.DisplayName + "<" + r.Address + ">").FirstOrDefault());
     request.Method = Method.POST;
     var response = client.Execute<dynamic>(request);
     Console.Write(response);
 }
コード例 #7
0
        public void Send(MailgunConfiguration configuration, MailMessage message)
        {
            var client = new RestClient
            {
                BaseUrl       = new Uri("https://api.mailgun.net/v2"),
                Authenticator = new HttpBasicAuthenticator("api", configuration.ApiKey)
            };
            RestRequest request = new RestRequest();

            request.AddParameter("domain", configuration.DomainName, ParameterType.UrlSegment);
            request.Resource = "{domain}/messages";
            request.AddParameter("from", message.From.DisplayName + " <" + message.From.Address + ">");
            request.AddParameter("to", string.Join(",", message.To.Select(t => t.Address).ToArray()));
            request.AddParameter("subject", message.Subject);
//            request.AddParameter("text", message.Body);
            request.AddParameter("html", message.Body);
            request.AddParameter("h:Reply-To", message.ReplyToList.Select(r => r.DisplayName + "<" + r.Address + ">").FirstOrDefault());
            request.Method = Method.POST;
            var response = client.Execute <dynamic>(request);

            Console.Write(response);
        }
コード例 #8
0
 public PartialViewResult EditAjax(MailgunConfiguration configuration)
 {
     var isValid = TryUpdateModel(configuration);
     if (!isValid)
         return PartialView("_MailgunConfigEdit", configuration);
     using (var session = DocumentStore.GetStore().OpenSession("Configuration"))
     {
         var mailgunConfiguration = session.Load<MailgunConfiguration>("MailgunConfig");
         if (mailgunConfiguration == null)
         {
             session.Store(configuration, "MailgunConfig");
         }
         else
         {
             mailgunConfiguration.ApiKey = configuration.ApiKey;
             mailgunConfiguration.DefaultFrom = configuration.DefaultFrom;
             mailgunConfiguration.DomainName = configuration.DomainName;
         }
         session.SaveChanges();
         return PartialView("_MailgunConfigDetails", configuration);
     }
 }
        public void CoordinatorCompleteWithEmailAndDefaultEmailSendsEmail()
        {
            var mailActioner = MockRepository.GenerateMock<IMailActioner>();
            var ravenDocStore = MockRepository.GenerateMock<IRavenDocStore>();
            var dateTimeMapper = MockRepository.GenerateMock<IDateTimeOlsenFromUtcMapping>();
            var session = MockRepository.GenerateMock<IDocumentSession>();
            ravenDocStore.Expect(r => r.GetStore().OpenSession("Configuration"))
                .Return(session);
            var mailgunConfig = new MailgunConfiguration { ApiKey = "key", DomainName = "domain", DefaultFrom = "*****@*****.**" };
            session.Expect(s => s.Load<MailgunConfiguration>("MailgunConfig")).Return(mailgunConfig);
            var emailDefaultNotification = new EmailDefaultNotification { EmailAddresses = new List<string> { "*****@*****.**", "*****@*****.**" } };
            session.Expect(s => s.Load<EmailDefaultNotification>("EmailDefaultConfig")).Return(emailDefaultNotification);

            var message = new MailMessage();
            mailActioner.Expect(m => m.Send(Arg<MailgunConfiguration>.Is.Equal(mailgunConfig), Arg<MailMessage>.Is.NotNull)).WhenCalled(a => message = (MailMessage)(a.Arguments[1]));
            dateTimeMapper.Expect(d => d.DateTimeUtcToLocalWithOlsenZone(Arg<DateTime>.Is.Anything, Arg<string>.Is.Anything)).Return(DateTime.Now).Repeat.Any();

            var emailService = new EmailService { MailActioner = mailActioner, RavenDocStore = ravenDocStore, DateTimeOlsenFromUtcMapping = dateTimeMapper };
            var coordinatorComplete = new CoordinatorCompleteEmailWithSummary
                                          {
                                              CoordinatorId = Guid.NewGuid(),
                                              EmailAddresses = new List<string> { "*****@*****.**", "*****@*****.**" },
                                              FinishTimeUtc = DateTime.Now,
                                              StartTimeUtc = DateTime.Now.AddMinutes(-10),
                                              FailedCount = 4,
                                              SuccessCount = 3,
                                              Cost = 33.44m
                                          };
            emailService.Handle(coordinatorComplete);

            mailActioner.VerifyAllExpectations();
            Assert.That(message.From.ToString(), Is.EqualTo(mailgunConfig.DefaultFrom));
            Assert.That(message.To[0].Address, Is.EqualTo(coordinatorComplete.EmailAddresses[0]));
            Assert.That(message.To[1].Address, Is.EqualTo(coordinatorComplete.EmailAddresses[1]));
            Assert.That(message.To[2].Address, Is.EqualTo(emailDefaultNotification.EmailAddresses[0]));
            Assert.That(message.To[3].Address, Is.EqualTo(emailDefaultNotification.EmailAddresses[1]));
        }
コード例 #10
0
ファイル: MailgunEmailSender.cs プロジェクト: ziyunhx/cicada
 public MailgunEmailSender(ILogger <MailgunEmailSender> logger, ISender client, MailgunConfiguration configuration)
 {
     _logger        = logger;
     _client        = client;
     _configuration = configuration;
 }
コード例 #11
0
 public Mailer(IOptions <MailgunConfiguration> mailgunConfiguration, ILogger <Mailer> logger)
 {
     _mailgunConfiguration = mailgunConfiguration?.Value ?? throw new ArgumentNullException(nameof(mailgunConfiguration));
     _logger = logger ?? throw new ArgumentNullException(nameof(logger));
 }
        public void CoordinatorCompleteWithEmailDefaultFromNotSentThrowsException()
        {
            var ravenDocStore = MockRepository.GenerateMock<IRavenDocStore>();
            var session = MockRepository.GenerateMock<IDocumentSession>();
            ravenDocStore.Expect(r => r.GetStore().OpenSession("Configuration"))
                .Return(session);
            var emailDefaultNotification = new EmailDefaultNotification { EmailAddresses = new List<string> { "*****@*****.**", "*****@*****.**" } };
            session.Expect(s => s.Load<EmailDefaultNotification>("EmailDefaultConfig")).Return(emailDefaultNotification);
            var mailgunConfig = new MailgunConfiguration { ApiKey = "key", DomainName = "domain", DefaultFrom = string.Empty };
            session.Expect(s => s.Load<MailgunConfiguration>("MailgunConfig")).Return(mailgunConfig);

            var emailService = new EmailService { RavenDocStore = ravenDocStore };
            var coordinatorComplete = new CoordinatorCompleteEmailWithSummary { EmailAddresses = new List<string> { "*****@*****.**" } };
            Assert.That(() => emailService.Handle(coordinatorComplete), Throws.Exception.With.Message.EqualTo("Could not find the default 'From' sender."));
        }
コード例 #13
0
 public Mailer(IOptions <MailgunConfiguration> mailgunConfiguration)
 {
     _mailgunConfiguration = mailgunConfiguration?.Value ?? throw new ArgumentNullException(nameof(mailgunConfiguration));
 }