From 45e86fd063dd80dd40336c75d0b0cfdd3a1519d3 Mon Sep 17 00:00:00 2001 From: Jonas Hinterdorfer Date: Tue, 11 Mar 2025 21:42:48 +0100 Subject: [PATCH] made async --- ConsoleApp1/MailService.cs | 97 ++++++++++++++++++-------------------- ConsoleApp1/Program.cs | 13 ++--- 2 files changed, 52 insertions(+), 58 deletions(-) diff --git a/ConsoleApp1/MailService.cs b/ConsoleApp1/MailService.cs index 545529a..e013b08 100644 --- a/ConsoleApp1/MailService.cs +++ b/ConsoleApp1/MailService.cs @@ -3,60 +3,32 @@ using System.Net.Mail; namespace ConsoleApp1; -public class MailService +public class MailService(SmtpSettings smtpSettings) { - private readonly SmtpSettings _smtpSettings; - - public MailService(SmtpSettings smtpSettings) + /// + /// Sends a plain text email to the specified recipient. + /// + /// The recipient's email address. + /// The subject of the email. + /// The body of the email. + public async Task SendEmailText(string to, string subject, string body) { - _smtpSettings = smtpSettings; + await SendEmailInternal(to, subject, body, isHtml: false); } - public void SendEmail(string to, string subject, string body) + /// + /// Sends an HTML email to the specified recipient using a file as the body. + /// + /// The recipient's email address. + /// The subject of the email. + /// The file path of the HTML file to be used as the body of the email. + /// Optional dictionary of placeholder replacements to be applied to the HTML body. + public async Task SendEmailHtmlFile(string to, string subject, string filePath, + Dictionary? replacements = null) { - using (var client = new SmtpClient(_smtpSettings.SmtpServer, _smtpSettings.SmtpPort)) - { - client.Credentials = new NetworkCredential(_smtpSettings.SmtpUser, _smtpSettings.SmtpPassword); - client.EnableSsl = true; + string body = await File.ReadAllTextAsync(filePath); - var mailMessage = new MailMessage - { - From = new MailAddress(_smtpSettings.SmtpFrom), - Subject = subject, - Body = body, - IsBodyHtml = true, - }; - - mailMessage.To.Add(to); - - client.Send(mailMessage); - } - } - - private void SendEmailHtml(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); - } - - public void SendEmailHtmlFile(string to, string subject, string filePath, Dictionary? replacements = null) - { - string body = System.IO.File.ReadAllText(filePath); - - if (replacements is not null) + if (replacements != null) { foreach (var replacement in replacements) { @@ -64,7 +36,32 @@ public class MailService } } - SendEmailHtml(to, subject, body); + await SendEmailInternal(to, subject, body, isHtml: true); + } + + /// + /// Sends an email to the specified recipient. + /// + /// The recipient's email address. + /// The subject of the email. + /// The body of the email. + /// Indicates whether the body of the email is HTML. + private async Task SendEmailInternal(string to, string subject, string body, bool isHtml) + { + 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 = isHtml + }; + + mailMessage.To.Add(to); + await client.SendMailAsync(mailMessage); } - } \ No newline at end of file diff --git a/ConsoleApp1/Program.cs b/ConsoleApp1/Program.cs index 7ef0632..a62a195 100644 --- a/ConsoleApp1/Program.cs +++ b/ConsoleApp1/Program.cs @@ -1,12 +1,10 @@ -using System; -using System.IO; -using System.Text.Json; +using System.Text.Json; namespace ConsoleApp1 { class Program { - static void Main(string[] args) + static async Task Main() { string jsonFilePath = "appsettings.json"; string jsonString = File.ReadAllText(jsonFilePath); @@ -25,16 +23,15 @@ namespace ConsoleApp1 } MailService mailService = new MailService(smtpSettings); - mailService.SendEmail("user@user.com", "Hello World", "Hello World from C#!"); - Console.WriteLine("Email sent!"); + await mailService.SendEmailText("user@user.com", "Hello World", "Hello World from C#!"); Dictionary replacements = new Dictionary { { "name", "Jonas" } }; - mailService.SendEmailHtmlFile("user@user.com" , "Hello World", "email.html", replacements); - Console.WriteLine("Email sent!"); + await mailService.SendEmailHtmlFile("user@user.com" , "Hello World", "email.html", replacements); + } }