25fb48ccf0
- 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.
82 lines
2.9 KiB
C#
82 lines
2.9 KiB
C#
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;
|
|
using HrynCo.NotificationService.Services.Logging;
|
|
using static HrynCo.NotificationService.Services.Core.ServiceResultHelper;
|
|
|
|
namespace HrynCo.NotificationService.Services.EmailChannels.Send;
|
|
|
|
internal sealed class SendEmailHandler
|
|
: RequestHandler<SendEmailCommand, ServiceResult<Core.Unit>>
|
|
{
|
|
private readonly IEmailChannelRepository _channels;
|
|
private readonly IEmailChannelUsageRepository _usage;
|
|
|
|
public SendEmailHandler(
|
|
IContextualSerilogLogger<SendEmailCommand> logger,
|
|
IEmailChannelRepository channels,
|
|
IEmailChannelUsageRepository usage)
|
|
: base(logger)
|
|
{
|
|
_channels = channels;
|
|
_usage = usage;
|
|
}
|
|
|
|
protected override async Task<ServiceResult<Core.Unit>> DoOnHandle(
|
|
SendEmailCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var channel = await _channels.GetByIdAsync(request.ChannelId, cancellationToken);
|
|
if (channel is null)
|
|
return Failure<Core.Unit>($"Channel '{request.ChannelId}' not found.");
|
|
|
|
if (channel.Settings is not SmtpChannelSettings smtp)
|
|
return Failure<Core.Unit>($"Channel type '{channel.EmailChannelType}' is not supported for sending.");
|
|
|
|
try
|
|
{
|
|
using var client = new SmtpClient(smtp.Host, smtp.Port)
|
|
{
|
|
EnableSsl = smtp.UseSsl,
|
|
Credentials = string.IsNullOrWhiteSpace(smtp.Username)
|
|
? null
|
|
: new NetworkCredential(smtp.Username, smtp.Password)
|
|
};
|
|
|
|
using var mail = new MailMessage
|
|
{
|
|
From = new MailAddress(smtp.FromEmail, smtp.FromName),
|
|
Subject = request.Subject,
|
|
Body = request.TextBody ?? string.Empty,
|
|
IsBodyHtml = false,
|
|
BodyEncoding = Encoding.UTF8,
|
|
SubjectEncoding = Encoding.UTF8
|
|
};
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.HtmlBody))
|
|
{
|
|
var html = AlternateView.CreateAlternateViewFromString(request.HtmlBody, Encoding.UTF8, "text/html");
|
|
mail.AlternateViews.Add(html);
|
|
}
|
|
|
|
mail.To.Add(new MailAddress(request.RecipientEmail, request.RecipientName));
|
|
|
|
await client.SendMailAsync(mail, cancellationToken);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Error(ex, "SMTP send failed for channel {ChannelId}", request.ChannelId);
|
|
return Failure<Core.Unit>(ex.Message);
|
|
}
|
|
|
|
await _usage.IncrementUsageAsync(
|
|
request.ChannelId,
|
|
DateOnly.FromDateTime(DateTime.UtcNow),
|
|
cancellationToken);
|
|
|
|
return Success(Unit.Value);
|
|
}
|
|
}
|