made async

This commit is contained in:
Jonas Hinterdorfer 2025-03-11 21:42:48 +01:00
parent a42cba1ec8
commit 45e86fd063
2 changed files with 52 additions and 58 deletions

View File

@ -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)
/// <summary>
/// Sends a plain text email to the specified recipient.
/// </summary>
/// <param name="to">The recipient's email address.</param>
/// <param name="subject">The subject of the email.</param>
/// <param name="body">The body of the email.</param>
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)
/// <summary>
/// Sends an HTML email to the specified recipient using a file as the body.
/// </summary>
/// <param name="to">The recipient's email address.</param>
/// <param name="subject">The subject of the email.</param>
/// <param name="filePath">The file path of the HTML file to be used as the body of the email.</param>
/// <param name="replacements">Optional dictionary of placeholder replacements to be applied to the HTML body.</param>
public async Task SendEmailHtmlFile(string to, string subject, string filePath,
Dictionary<string, string>? 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<string, string>? 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);
}
/// <summary>
/// Sends an email to the specified recipient.
/// </summary>
/// <param name="to">The recipient's email address.</param>
/// <param name="subject">The subject of the email.</param>
/// <param name="body">The body of the email.</param>
/// <param name="isHtml">Indicates whether the body of the email is HTML.</param>
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);
}
}

View File

@ -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<string, string> replacements = new Dictionary<string, string>
{
{ "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);
}
}