using System.Net.Http; using System.Text; using System.Text.Json; namespace RadicadeServer.Radicade.Mail; public static class RadicadeMail { private static readonly HttpClient Http = new(); private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; public static async Task SendAsync(string to, string subject, string html) { if (string.IsNullOrWhiteSpace(DBConfig.MailApiKey)) return; if (string.IsNullOrWhiteSpace(to) || !to.Contains('@')) return; if (string.IsNullOrWhiteSpace(DBConfig.MailFrom)) return; try { var payload = new { from = $"{DBConfig.MailFromName} <{DBConfig.MailFrom}>", to = new[] { to }, subject, html }; using var req = new HttpRequestMessage(HttpMethod.Post, DBConfig.MailApiUrl) { Headers = { Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", DBConfig.MailApiKey) }, Content = new StringContent(JsonSerializer.Serialize(payload, JsonOptions), Encoding.UTF8, "application/json") }; using var res = await Http.SendAsync(req); } catch (Exception) { } } }