refactor: modularize email processing logic and improve service structure

- Extract email template handling, rendering, and sending code into `Worker.Services` project.
- Introduce `EmailTemplateService`, `EmailTemplateRenderingService`, and `SendEmailService`.
- Simplify consumer logic by delegating to scoped services.
- Update project dependencies and package references accordingly.
This commit is contained in:
Anatolii Grynchuk
2026-05-14 22:15:15 +03:00
parent 0861e18cec
commit 25fb48ccf0
18 changed files with 394 additions and 174 deletions
@@ -1,5 +1,6 @@
using System.Net;
using System.Net.Mail;
using System.Text;
using HrynCo.NotificationService.DAL.Abstract.Providers;
using HrynCo.NotificationService.DAL.Abstract.Repositories;
using HrynCo.NotificationService.Services.Core;
@@ -48,14 +49,16 @@ internal sealed class SendEmailHandler
{
From = new MailAddress(smtp.FromEmail, smtp.FromName),
Subject = request.Subject,
Body = request.HtmlBody,
IsBodyHtml = true
Body = request.TextBody ?? string.Empty,
IsBodyHtml = false,
BodyEncoding = Encoding.UTF8,
SubjectEncoding = Encoding.UTF8
};
if (!string.IsNullOrWhiteSpace(request.TextBody))
if (!string.IsNullOrWhiteSpace(request.HtmlBody))
{
var plain = AlternateView.CreateAlternateViewFromString(request.TextBody, null, "text/plain");
mail.AlternateViews.Add(plain);
var html = AlternateView.CreateAlternateViewFromString(request.HtmlBody, Encoding.UTF8, "text/html");
mail.AlternateViews.Add(html);
}
mail.To.Add(new MailAddress(request.RecipientEmail, request.RecipientName));