Created
February 2, 2023 04:05
-
-
Save Dark-Rex01/2b0f82c4761d18cffaf926a7548632f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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