public void Dependency_injection_generic_host() { var serviceCollection = new ServiceCollection(); serviceCollection.AddOptions(); var viewEngine = new Mock <IRazorViewEngine>(); var tempDataProvider = new Mock <ITempDataProvider>(); var hostingEnvironment = new Mock <Microsoft.Extensions.Hosting.IHostEnvironment>(); var logger = new Mock <ILogger <EmailService> >(); var logger2 = new Mock <ILogger <TemplateService> >(); serviceCollection.AddSingleton(logger.Object); serviceCollection.AddSingleton(logger2.Object); serviceCollection.AddSingleton(viewEngine.Object); serviceCollection.AddSingleton(tempDataProvider.Object); serviceCollection.AddSingleton <Microsoft.Extensions.Hosting.IHostEnvironment>(hostingEnvironment.Object); serviceCollection.Configure <EmailServiceOptions>(o => { o.CreateSmtpClient = () => throw new FactExcetpionForSmtpCreation(); }); serviceCollection.AddPostal(); var services = serviceCollection.BuildServiceProvider(); var emailService = services.GetRequiredService <IEmailService>(); Assert.ThrowsAsync <FactExcetpionForSmtpCreation>(() => emailService.SendAsync(new Email("testView"))); EmailServiceOptions emailOptionField = GetInstanceField(typeof(EmailService), emailService, "options") as EmailServiceOptions; Assert.Throws <FactExcetpionForSmtpCreation>(() => emailOptionField.CreateSmtpClient()); }
/// <summary> /// Send an email asynchronously, using an <see cref="SmtpClient"/>. /// </summary> /// <param name="email">The email to send.</param> /// <returns>A <see cref="Task"/> that completes once the email has been sent.</returns> public async Task SendAsync(MailMessage mailMessage) { using (var smtp = options.CreateSmtpClient()) { this.logger.LogDebug($"Smtp created: host: {smtp.Host}, port: {smtp.Port}, enableSsl: {smtp.EnableSsl}, defaultCredentials: {smtp.UseDefaultCredentials}"); this.logger.LogInformation($"Smtp send email from {mailMessage.From} to {mailMessage.To}"); await smtp.SendMailAsync(mailMessage); } }
public void Dependency_injection_smtpOtions2() { var serviceCollection = new ServiceCollection(); serviceCollection.AddOptions(); var viewEngine = new Mock <IRazorViewEngine>(); var tempDataProvider = new Mock <ITempDataProvider>(); var hostingEnvironment = new Mock <IWebHostEnvironment>(); var logger = new Mock <ILogger <EmailService> >(); var logger2 = new Mock <ILogger <TemplateService> >(); serviceCollection.AddSingleton(logger.Object); serviceCollection.AddSingleton(logger2.Object); serviceCollection.AddSingleton(viewEngine.Object); serviceCollection.AddSingleton(tempDataProvider.Object); serviceCollection.AddSingleton <IWebHostEnvironment>(hostingEnvironment.Object); serviceCollection.AddSingleton <Microsoft.Extensions.Hosting.IHostEnvironment>(hostingEnvironment.Object); serviceCollection.Configure <EmailServiceOptions>(o => { o.Host = "abc"; o.Port = 12345; o.FromAddress = "qwerty"; o.UserName = "******"; o.Password = "******"; o.CreateSmtpClient = () => new FactExcetpionForSmtpClient(); }); serviceCollection.AddPostal(); var services = serviceCollection.BuildServiceProvider(); var emailService = services.GetRequiredService <IEmailService>(); EmailServiceOptions emailOptionField = GetInstanceField(typeof(EmailService), emailService, "options") as EmailServiceOptions; emailOptionField.Host.ShouldBe("abc"); emailOptionField.Port.ShouldBe(12345); emailOptionField.FromAddress.ShouldBe("qwerty"); emailOptionField.UserName.ShouldBe("zxcvbn"); emailOptionField.Password.ShouldBe("asdfgh"); emailOptionField.CreateSmtpClient().ShouldBeOfType <FactExcetpionForSmtpClient>(); }