implement some html stuff

This commit is contained in:
Jonas Hinterdorfer 2025-03-11 21:29:30 +01:00
parent 0fd83b7fad
commit 86c7b6bb8d
4 changed files with 110 additions and 0 deletions

View File

@ -11,6 +11,9 @@
<None Update="appsettings.json"> <None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>
<None Update="email.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -32,4 +32,39 @@ public class MailService
client.Send(mailMessage); 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)
{
foreach (var replacement in replacements)
{
body = body.Replace($"{{{replacement.Key}}}", replacement.Value);
}
}
SendEmailHtml(to, subject, body);
}
} }

View File

@ -26,6 +26,15 @@ namespace ConsoleApp1
MailService mailService = new MailService(smtpSettings); MailService mailService = new MailService(smtpSettings);
mailService.SendEmail("user@user.com", "Hello World", "Hello World from C#!"); mailService.SendEmail("user@user.com", "Hello World", "Hello World from C#!");
Console.WriteLine("Email sent!");
Dictionary<string, string> replacements = new Dictionary<string, string>
{
{ "name", "Jonas" }
};
mailService.SendEmailHtmlFile("user@user.com" , "Hello World", "email.html", replacements);
Console.WriteLine("Email sent!");
} }
} }

63
ConsoleApp1/email.html Normal file
View File

@ -0,0 +1,63 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Email Template</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
}
.container {
width: 100%;
max-width: 600px;
margin: 0 auto;
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.header {
text-align: center;
padding: 10px 0;
border-bottom: 1px solid #dddddd;
}
.header h1 {
margin: 0;
color: #333333;
}
.content {
padding: 20px;
}
.content p {
line-height: 1.6;
color: #666666;
}
.footer {
text-align: center;
padding: 10px 0;
border-top: 1px solid #dddddd;
color: #999999;
font-size: 12px;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>Welcome to Our Service</h1>
</div>
<div class="content">
<p>Dear {name},</p>
<p>Thank you for signing up for our service. We are excited to have you on board.</p>
<p>If you have any questions, feel free to contact us at any time.</p>
<p>Best regards,<br>The Team</p>
</div>
<div class="footer">
<p>&copy; 2023 Our Service. All rights reserved.</p>
</div>
</div>
</body>
</html>