From 5a821b8d53daacb81a3cf0708d11f37613e095a5 Mon Sep 17 00:00:00 2001 From: Jammy96 <143798376+Jammy96@users.noreply.github.com> Date: Mon, 28 Oct 2024 20:54:06 +0530 Subject: [PATCH] Update SMTPEmailService.cs Hacktoberfest --- .../Helpers/MailService/SMTPEmailService.cs | 53 ++++++++++++------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/Services/Helpers/MailService/SMTPEmailService.cs b/Services/Helpers/MailService/SMTPEmailService.cs index 297c405..11d8167 100644 --- a/Services/Helpers/MailService/SMTPEmailService.cs +++ b/Services/Helpers/MailService/SMTPEmailService.cs @@ -1,16 +1,29 @@ -using System; +using System; +using System.Threading.Tasks; using MailKit.Net.Smtp; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using MimeKit; using Models.Helpers; -namespace Services.Helpers.MailService; - -public class SmtpMailService(IOptions json) : IMailService +namespace Services.Helpers.MailService { - public void SendEmail(string toEmail, string message, string fromTitle = "", string Subject = "") + public class SmtpMailService : IMailService { - var options = json.Value.Smtp; + private readonly AppSettings _appSettings; // Dependency injected settings + private readonly ILogger _logger; // Dependency injected logger for better logging + + // Constructor with dependency injection + public SmtpMailService(IOptions settings, ILogger logger) + { + _appSettings = settings.Value; // Assign settings from IOptions + _logger = logger; // Assign logger + } + + // Asynchronous method to send email + public async Task SendEmailAsync(string toEmail, string message, string fromTitle = "", string subject = "") + { + var options = _appSettings.Smtp; toEmail = toEmail.ToLower(); var email = new MimeMessage(); var from = new MailboxAddress(fromTitle, options.Email); @@ -19,29 +32,29 @@ public void SendEmail(string toEmail, string message, string fromTitle = "", str var to = MailboxAddress.Parse(toEmail); email.To.Add(to); - email.Subject = Subject; + email.Subject = subject; + // Supporting HTML content in the body var bodyBuilder = new BodyBuilder { - TextBody = message + TextBody = message, + HtmlBody = $"{message}" }; email.Body = bodyBuilder.ToMessageBody(); - var client = new SmtpClient(); + + using var client = new SmtpClient(); // Using statement to ensure disposal try { - client.Connect(options.Host, options.Port, options.UseSsl); - client.Authenticate(options.Email, options.Password); - client.Send(email); - client.Disconnect(true); + await client.ConnectAsync(options.Host, options.Port, options.UseSsl); // Asynchronous connection + await client.AuthenticateAsync(options.Email, options.Password); // Asynchronous authentication + await client.SendAsync(email); // Asynchronous send + await client.DisconnectAsync(true); // Asynchronous disconnect } catch (Exception e) { - Console.WriteLine(e); - throw; - } - finally - { - client.Dispose(); + _logger.LogError("Failed to send email: {ExceptionMessage}", e.Message); // Detailed logging + throw new ApplicationException("Error in sending email", e); // Throwing an application-specific exception } } -} \ No newline at end of file + } +}