Send-Email-CSharp/ConsoleApp1/MailService.cs
2025-03-11 21:14:37 +01:00

35 lines
897 B
C#

using System.Net;
using System.Net.Mail;
namespace ConsoleApp1;
public class MailService
{
private readonly SmtpSettings _smtpSettings;
public MailService(SmtpSettings smtpSettings)
{
_smtpSettings = smtpSettings;
}
public void SendEmail(string to, string subject, string body)
{
using (var client = new SmtpClient(_smtpSettings.SmtpServer, _smtpSettings.SmtpPort))
{
client.Credentials = new NetworkCredential(_smtpSettings.SmtpUser, _smtpSettings.SmtpPassword);
client.EnableSsl = true;
var mailMessage = new MailMessage
{
From = new MailAddress(_smtpSettings.SmtpFrom),
Subject = subject,
Body = body,
IsBodyHtml = true,
};
mailMessage.To.Add(to);
client.Send(mailMessage);
}
}
}