Skip to content

Instantly share code, notes, and snippets.

@Dark-Rex01
Created February 2, 2023 04:05
Show Gist options
  • Select an option

  • Save Dark-Rex01/2b0f82c4761d18cffaf926a7548632f3 to your computer and use it in GitHub Desktop.

Select an option

Save Dark-Rex01/2b0f82c4761d18cffaf926a7548632f3 to your computer and use it in GitHub Desktop.
namespace Final.MailServices
{
public class MailService : IMailService
{
private readonly IConfiguration _configuration;
public MailService(IConfiguration configuration)
{
_configuration = configuration;
}
public async Task Send(string ToAddress, string subject, string body)
{
string? SmtpServer = _configuration.GetSection("MailSettings:SmtpHost").Value;
int Port = int.Parse(_configuration.GetSection("MailSettings:Port").Value);
string? FromAddress = _configuration.GetSection("MailSettings:MailAddress").Value;
string? Password = _configuration.GetSection("MailSettings:Password").Value;
var email = new MimeMessage();
email.From.Add(MailboxAddress.Parse(FromAddress));
email.To.Add(MailboxAddress.Parse(ToAddress));
email.Subject = subject;
email.Body = new TextPart(MimeKit.Text.TextFormat.Html) { Text = body };
using (var smtp = new SmtpClient())
{
smtp.Connect(SmtpServer, Port);
smtp.Authenticate(
FromAddress,
Password
);
await smtp.SendAsync(email);
smtp.Disconnect(true);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment