using System.Net; using System.Net.Mail; using HrynCo.DAL.Abstract; using HrynCo.NotificationService.Services.Core; using HrynCo.NotificationService.Services.Logging; using static HrynCo.NotificationService.Services.Core.ServiceResultHelper; namespace HrynCo.NotificationService.Services.EmailChannels.TestSmtp; internal sealed class TestSmtpHandler : RequestHandler> { public TestSmtpHandler( IContextualSerilogLogger logger, IUnitOfWork unitOfWork) : base(logger, unitOfWork) { } protected override async Task> DoOnHandle( TestSmtpCommand request, CancellationToken cancellationToken) { try { using var client = new SmtpClient(request.Host, request.Port) { EnableSsl = request.UseSsl, Credentials = string.IsNullOrWhiteSpace(request.Username) ? null : new NetworkCredential(request.Username, request.Password) }; using var mail = new MailMessage { From = new MailAddress(request.FromEmail, request.FromName), Subject = "✅ Test email from Notification Service", Body = "

This is a test email sent from the Notification Service admin panel to verify the channel settings.

", IsBodyHtml = true }; mail.To.Add(new MailAddress(request.ToEmail)); await client.SendMailAsync(mail, cancellationToken); } catch (Exception ex) { Logger.Error(ex, "Ad-hoc SMTP test failed for host {Host}", request.Host); return Failure(ex.Message); } return Success(Unit.Value); } }