Files
AlquilaFacil/Aspnet/Emailer/Decorator/EmailerSender.cs

51 lines
1.4 KiB
C#

using System.Net.Mail;
using System.Net;
using System.Text.Json;
namespace AlquilaFacil.Emailer.Sender;
public class EmailSender
{
protected static SmtpClient? smtp = null;
protected void configSmtp(MailMessage mail)
{
var jsonContent = File.ReadAllText("settings.json");
var options = JsonSerializer.Deserialize<Dictionary<string, string>>(jsonContent);
if (options == null) return;
bool check = options.ContainsKey("smtpHost");
check = options.ContainsKey("smtpPort");
check = options.ContainsKey("emailAddr");
check = options.ContainsKey("emailPass");
if (check == false) return;
mail.Sender = new MailAddress(options["emailAddr"]);
mail.From = new MailAddress(options["emailAddr"]);
if (null != smtp) return;
smtp = new();
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
smtp.Host = options["smtpHost"];
smtp.Port = int.Parse(options["smtpPort"].ToString());
smtp.Credentials = new NetworkCredential(options["emailAddr"], options["emailPass"]);
}
public virtual void Send(MailMessage message)
{
configSmtp(message);
if (smtp == null) return;
try
{
smtp.Send(message);
message.Dispose();
}
catch (Exception)
{
throw;
}
}
}