namespace LaaProductionWeb.API.Models.SMTP { using System; using System.Collections.Generic; using System.IO; using System.Net.Mail; using System.Threading.Tasks; public class SMTPClient { private readonly SMTPSettings settings; public SMTPClient(SMTPSettings settings) => this.settings = settings; public async Task SendAsync(string subject, string body, string from, IEnumerable to) { try { using (var client = new SmtpClient(settings.Host, settings.Port)) { client.Host = this.settings.Host; client.Port = this.settings.Port; client.UseDefaultCredentials = true; var message = new MailMessage { From = new MailAddress(from), Subject = subject, Body = body, IsBodyHtml = true, }; foreach (var email in to) { message.To.Add(email); } await client.SendMailAsync(message); } return true; } catch (Exception e) { File.AppendAllLines("smtp.log", new[] { $"[SMTP_ERROR] {e.Message}" }); return false; } } } }