diff --git a/coffeecard/CoffeeCard.Library/CoffeeCard.Library.csproj b/coffeecard/CoffeeCard.Library/CoffeeCard.Library.csproj index afccfeee..a2b42c13 100644 --- a/coffeecard/CoffeeCard.Library/CoffeeCard.Library.csproj +++ b/coffeecard/CoffeeCard.Library/CoffeeCard.Library.csproj @@ -10,7 +10,7 @@ - + diff --git a/coffeecard/CoffeeCard.Library/Services/MailgunEmailSender.cs b/coffeecard/CoffeeCard.Library/Services/MailgunEmailSender.cs index 227b778c..b44d664e 100644 --- a/coffeecard/CoffeeCard.Library/Services/MailgunEmailSender.cs +++ b/coffeecard/CoffeeCard.Library/Services/MailgunEmailSender.cs @@ -7,12 +7,10 @@ namespace CoffeeCard.Library.Services; -public class MailgunEmailSender(MailgunSettings mailgunSettings) : IEmailSender +public class MailgunEmailSender(MailgunSettings mailgunSettings, IRestClient restClient) : IEmailSender { public async Task SendEmailAsync(MimeMessage mail) { - using var client = new RestClient(mailgunSettings.MailgunApiUrl); - client.Authenticator = new HttpBasicAuthenticator("api", mailgunSettings.ApiKey); var request = new RestRequest(); request.AddParameter("domain", mailgunSettings.Domain, ParameterType.UrlSegment); @@ -23,12 +21,16 @@ public async Task SendEmailAsync(MimeMessage mail) request.AddParameter("html", mail.HtmlBody); request.Method = Method.Post; - var response = await client.ExecutePostAsync(request); + var response = await restClient.ExecutePostAsync(request); if (!response.IsSuccessful) { - Log.Error("Error sending request to Mailgun. StatusCode: {statusCode} ErrorMessage: {errorMessage}", - response.StatusCode, response.ErrorMessage); + Log.Error( + "Error sending request to Mailgun. StatusCode: {statusCode} ErrorMessage: {errorMessage}", + response.StatusCode, + response.ErrorMessage + ); } } -} \ No newline at end of file +} + diff --git a/coffeecard/CoffeeCard.WebApi/Startup.cs b/coffeecard/CoffeeCard.WebApi/Startup.cs index e227dc7e..46468c78 100644 --- a/coffeecard/CoffeeCard.WebApi/Startup.cs +++ b/coffeecard/CoffeeCard.WebApi/Startup.cs @@ -31,6 +31,8 @@ using NJsonSchema.Generation; using NSwag; using NSwag.Generation.Processors.Security; +using RestSharp; +using RestSharp.Authenticators; using AccountService = CoffeeCard.Library.Services.AccountService; using IAccountService = CoffeeCard.Library.Services.IAccountService; using IPurchaseService = CoffeeCard.Library.Services.IPurchaseService; @@ -95,6 +97,16 @@ public void ConfigureServices(IServiceCollection services) services.AddScoped(); services.AddSingleton(_environment.ContentRootFileProvider); + services.AddSingleton(provider => + { + var mailgunSettings = provider.GetRequiredService(); + var options = new RestClientOptions(mailgunSettings.MailgunApiUrl) + { + Authenticator = new HttpBasicAuthenticator("api", mailgunSettings.ApiKey), + }; + return new RestClient(options); + }); + services.AddScoped(); services.AddScoped(); services.AddMobilePayHttpClients(_configuration.GetSection("MobilePaySettingsV2").Get());