public void Configure(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddCors(); services.AddDbContext <VideoGamerDbContext>(config => { config.UseSqlServer(_configuration.GetConnectionString("VideoGamer")); }); services.AddTransient <IUserService, EFUserService>() .AddTransient <IDeveloperService, EFDeveloperService>() .AddTransient <IGameService, EFGameService>() .AddTransient <IGenreService, EFGenreService>() .AddTransient <IRegisterService, EFRegisterService>() .AddTransient <ILoginService, EFLoginService>() .AddTransient <IPublisherService, EFPublisherService>(); services.AddSingleton <IFileService, FileUploadService>(); var section = _configuration.GetSection("Email"); var sender = new SmtpEmailService( section["host"], Int32.Parse(section["port"]), section["fromaddress"], section["password"] ); services.AddSingleton <IEmailService>(sender); services.AddSingleton <IPasswordHasher, PasswordHasher>((service) => new PasswordHasher(new RNGCryptoServiceProvider())); }
public void SendEmail_InvalidInput_InvalidEmailTo2() { var emailservice = new SmtpEmailService(_mockedLog.Object, _mockSmtpEmailService.Object); _mockSmtpEmailService.Setup(m => m.Send(It.IsAny <MailMessage>())); Assert.IsFalse(emailservice.SendEmail("foo", "bar", "*****@*****.**", null, null, null)); }
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); }
private static void Initialise() { _random = new Random(); _availableServices = new Dictionary <string, Func <ServiceSettings, DeliveryService> >(); _deliveryServices = new List <DeliveryService>(); _emailService = new SmtpEmailService(ConfigurationManager.AppSettings["smtphost"], ConfigurationManager.AppSettings["emailUsername"], ConfigurationManager.AppSettings["emailPassword"]); SetAvailableServices(); LoadServices(); }
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); }
public void SendEmail_OtherException() { var emailservice = new SmtpEmailService(_mockedLog.Object, _mockSmtpEmailService.Object); var recipients = new List <string> { "*****@*****.**" }; _mockSmtpEmailService.Setup(m => m.Send(It.IsAny <MailMessage>())).Throws(new ArgumentNullException()); Assert.IsFalse(emailservice.SendEmail("foo", "bar", "*****@*****.**", recipients, null, null)); }
public void SendEmail_ValidInputs_SMTPFormat2() { var emailservice = new SmtpEmailService(_mockedLog.Object, _mockSmtpEmailService.Object); var recipients = new List <string> { "*****@*****.**" }; _mockSmtpEmailService.Setup(m => m.Send(It.IsAny <MailMessage>())); Assert.IsTrue(emailservice.SendEmail("foo", "bar", "\"testBar(test)\"<*****@*****.**>", recipients, null, null)); }
public void SendEmail_ValidInputs_BadDomain_WithDefault() { var emailservice = new SmtpEmailService(_mockedLog.Object, _mockSmtpEmailService.Object); var recipients = new List <string> { "*****@*****.**" }; _mockSmtpEmailService.Setup(m => m.Send(It.IsAny <MailMessage>())); Assert.IsTrue(emailservice.SendEmail("foo", "bar", "*****@*****.**", recipients, null, null)); }
public void SendEmail_InvalidInput_InvalidEmailTo4() { var emailservice = new SmtpEmailService(_mockedLog.Object, _mockSmtpEmailService.Object); var recipients = new List <string> { "*****@*****.**", "foobar" }; _mockSmtpEmailService.Setup(m => m.Send(It.IsAny <MailMessage>())); Assert.IsFalse(emailservice.SendEmail("foo", "bar", "*****@*****.**", recipients, null, null)); }
public void SendEmail_ValidInputs_HTMLBody() { var emailservice = new SmtpEmailService(_mockedLog.Object, _mockSmtpEmailService.Object); var recipients = new List <string> { "*****@*****.**" }; _mockSmtpEmailService.Setup(m => m.Send(It.IsAny <MailMessage>())); Assert.IsTrue(emailservice.SendEmail("foo", "<html>Foo</html>", "*****@*****.**", recipients, null, null)); }
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); }); }
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); }); }
public async Task SendEmailShouldReturnEmailMessage() { var configuration = TestsPrerequisites.GetConfiguration(); var emailService = new SmtpEmailService(configuration); var receiver = "*****@*****.**"; var subject = "Xunit test"; var message = "Xunit test message"; var sender = "LearningPlus Tests"; var emailMessage = await emailService.SendEmailAsync(receiver, subject, message, sender); Assert.Equal(message, emailMessage.Body); }
public void SendEmail_ValidInputs_withInvalidAttachments5() { var emailservice = new SmtpEmailService(_mockedLog.Object, _mockSmtpEmailService.Object); var recipients = new List <string> { "*****@*****.**" }; var attachments = new List <FileAttachment>(); attachments.Add(new FileAttachment { Content = " ", Name = "bar" }); _mockSmtpEmailService.Setup(m => m.Send(It.IsAny <MailMessage>())); Assert.IsFalse(emailservice.SendEmail("foo", "bar", "*****@*****.**", recipients, recipients, attachments)); }
public async Task <IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = new ApplicationUser() { UserName = model.Email, Email = model.Email }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (!result.Succeeded) { return(GetErrorResult(result)); } //this sends a congratulatory Email SmtpEmailService messageSvc = new SmtpEmailService(); string envPath = HttpRuntime.AppDomainAppPath; string htmlBody = File.ReadAllText($"{envPath}\\Messaging\\Email.html"); htmlBody = htmlBody.Replace("$customerName$", model.Email).Replace("$emailAddress$", model.Email).Replace("$password$", model.Password); EmailMessage msg = new EmailMessage() { Body = htmlBody, Subject = "Welcome To Find Jobs Fast", Recipient = model.Email }; messageSvc.SendEmail(msg); //adds admin and set up the admin role string email = "*****@*****.**"; string password = "******"; string role = "ADMIN"; var roleMgr = Startup.RoleManagerFactory.Invoke(); if (!roleMgr.RoleExists(role)) { var irole = new IdentityRole() { Name = role }; roleMgr.Create(irole); } bool check = UserManager.IsInRole(user.Id, role); if (!check && email == model.Email && password == model.Password) { IdentityResult roleResult = UserManager.AddToRole(user.Id, role); } return(Ok()); }
public void SendEmail_DefaultSmptpService() { var emailservice = new SmtpEmailService(_mockedLog.Object); Assert.IsNotNull(emailservice); }
public void FirstSignature() { var mockSmtpClient = new Mock<ISmtpClient>(); var emailService = new SmtpEmailService(mockSmtpClient.Object); mockSmtpClient.Setup(x => x.Send(It.IsAny<MailMessage>())).Callback((MailMessage mailMessage) => { mailMessage.To.Any(x => x.Address == _to).Should().BeTrue(); mailMessage.Subject.Should().Be(_subject); mailMessage.Body.Should().Be(_body); mailMessage.IsBodyHtml.Should().BeFalse(); }); emailService.Send(_to, _subject, _body); mockSmtpClient.VerifyAll(); mockSmtpClient.Setup(x => x.Send(It.IsAny<MailMessage>())).Callback((MailMessage mailMessage) => { mailMessage.To.Any(x => x.Address == _to).Should().BeTrue(); mailMessage.Subject.Should().Be(_subject); mailMessage.Body.Should().Be(_body); mailMessage.IsBodyHtml.Should().BeTrue(); }); emailService.Send(_to, _subject, _body, true); mockSmtpClient.VerifyAll(); mockSmtpClient.Setup(x => x.Send(It.IsAny<MailMessage>())).Callback((MailMessage mailMessage) => { mailMessage.To.Any(x => x.Address == _to).Should().BeTrue(); mailMessage.Subject.Should().Be(_subject); mailMessage.Body.Should().Be(_body); mailMessage.IsBodyHtml.Should().BeFalse(); mailMessage.Attachments.Any(x => x == _attachment).Should().BeTrue(); }); emailService.Send(_to, _subject, _body, new[] { _attachment }); mockSmtpClient.VerifyAll(); mockSmtpClient.Setup(x => x.Send(It.IsAny<MailMessage>())).Callback((MailMessage mailMessage) => { mailMessage.To.Any(x => x.Address == _to).Should().BeTrue(); mailMessage.Subject.Should().Be(_subject); mailMessage.Body.Should().Be(_body); mailMessage.IsBodyHtml.Should().BeTrue(); mailMessage.Attachments.Any(x => x == _attachment).Should().BeTrue(); }); emailService.Send(_to, _subject, _body, new[] { _attachment }, true); mockSmtpClient.VerifyAll(); mockSmtpClient.Setup(x => x.Send(It.IsAny<MailMessage>())).Callback((MailMessage mailMessage) => { mailMessage.To.Any(x => x.Address == _to).Should().BeTrue(); mailMessage.Subject.Should().Be(_subject); mailMessage.Body.Should().Be(_body); mailMessage.IsBodyHtml.Should().BeFalse(); }); emailService.Send(new[] {_to}, _subject, _body); mockSmtpClient.VerifyAll(); }