Files
Shogi/Shogi.Api/Repositories/EmailSender.cs
Lucas Morgan 51d234d871 Replace custom socket implementation with SignalR.
Replace MSAL and custom cookie auth with Microsoft.Identity.EntityFramework
Also some UI redesign to accommodate different login experience.
2024-08-25 03:46:44 +00:00

55 lines
1.4 KiB
C#

using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.Extensions.Options;
using System.Net.Http.Headers;
using System.Text.Json;
namespace Shogi.Api.Repositories;
// https://app-smtp.brevo.com/real-time
public class EmailSender : IEmailSender
{
private static readonly JsonSerializerOptions Options = new(JsonSerializerDefaults.Web);
private readonly HttpClient client;
private string apiKey;
public EmailSender(HttpClient client, IOptionsMonitor<ApiKeys> apiKeys)
{
this.apiKey = apiKeys.CurrentValue.BrevoEmailService;
apiKeys.OnChange(keys => this.apiKey = keys.BrevoEmailService);
this.client = client;
}
public async Task SendEmailAsync(string email, string subject, string htmlMessage)
{
var body = new
{
Sender = new
{
Name = "Shogi Account Support",
Email = "shogi@lucaserver.space",
},
To = new[]
{
new
{
Name = email,
Email = email,
}
},
Subject = subject,
HtmlContent = htmlMessage,
};
var request = new HttpRequestMessage(HttpMethod.Post, new Uri("https://api.brevo.com/v3/smtp/email", UriKind.Absolute));
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Add("api-key", apiKey);
request.Content = JsonContent.Create(body, options: Options);
var response = await this.client.SendAsync(request);
response.EnsureSuccessStatusCode();
}
}