laatzen/LaaProductionWeb/LaaProductionWeb.API/Models/SMTP/SMTPClient.cs
2023-05-17 15:54:15 +02:00

53 lines
1.5 KiB
C#

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<bool> SendAsync(string subject, string body, string from, IEnumerable<string> 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;
}
}
}
}