diff --git a/ConsoleApp1/ConsoleApp1.csproj b/ConsoleApp1/ConsoleApp1.csproj index c2d29fd..93ed4bf 100644 --- a/ConsoleApp1/ConsoleApp1.csproj +++ b/ConsoleApp1/ConsoleApp1.csproj @@ -11,6 +11,9 @@ Always + + Always + diff --git a/ConsoleApp1/MailService.cs b/ConsoleApp1/MailService.cs index 20d056c..545529a 100644 --- a/ConsoleApp1/MailService.cs +++ b/ConsoleApp1/MailService.cs @@ -32,4 +32,39 @@ public class MailService 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) + { + foreach (var replacement in replacements) + { + body = body.Replace($"{{{replacement.Key}}}", replacement.Value); + } + } + + SendEmailHtml(to, subject, body); + } + } \ No newline at end of file diff --git a/ConsoleApp1/Program.cs b/ConsoleApp1/Program.cs index e046fe0..7ef0632 100644 --- a/ConsoleApp1/Program.cs +++ b/ConsoleApp1/Program.cs @@ -26,6 +26,15 @@ namespace ConsoleApp1 MailService mailService = new MailService(smtpSettings); mailService.SendEmail("user@user.com", "Hello World", "Hello World from C#!"); + Console.WriteLine("Email sent!"); + + Dictionary replacements = new Dictionary + { + { "name", "Jonas" } + }; + + mailService.SendEmailHtmlFile("user@user.com" , "Hello World", "email.html", replacements); + Console.WriteLine("Email sent!"); } } diff --git a/ConsoleApp1/email.html b/ConsoleApp1/email.html new file mode 100644 index 0000000..58fb737 --- /dev/null +++ b/ConsoleApp1/email.html @@ -0,0 +1,63 @@ + + + + + Email Template + + + +
+
+

Welcome to Our Service

+
+
+

Dear {name},

+

Thank you for signing up for our service. We are excited to have you on board.

+

If you have any questions, feel free to contact us at any time.

+

Best regards,
The Team

+
+ +
+ + \ No newline at end of file