45 lines
1.3 KiB
C#
45 lines
1.3 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using System.Net.Mail;
|
|
using System.Net;
|
|
|
|
namespace PolyclinicBusinessLogic.BusinessLogics
|
|
{
|
|
public class SendMailLogic
|
|
{
|
|
private readonly IConfiguration _configuration;
|
|
|
|
public SendMailLogic(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
}
|
|
|
|
public void SendEmail(string toEmail, string subject, string attachmentPath)
|
|
{
|
|
var smtpSection = _configuration.GetSection("Smtp");
|
|
|
|
var client = new SmtpClient(smtpSection["Host"])
|
|
{
|
|
Port = int.Parse(smtpSection["Port"]),
|
|
Credentials = new NetworkCredential(smtpSection["Username"], smtpSection["Password"]),
|
|
EnableSsl = bool.Parse(smtpSection["EnableSsl"]),
|
|
};
|
|
|
|
var mailMessage = new MailMessage
|
|
{
|
|
From = new MailAddress(smtpSection["Username"]),
|
|
Subject = subject,
|
|
};
|
|
|
|
mailMessage.To.Add(toEmail);
|
|
|
|
if (!string.IsNullOrEmpty(attachmentPath))
|
|
{
|
|
Attachment attachment = new Attachment(attachmentPath);
|
|
mailMessage.Attachments.Add(attachment);
|
|
}
|
|
|
|
client.Send(mailMessage);
|
|
}
|
|
}
|
|
}
|