< Summary

Information
Class: SlqStudio.Application.Services.EmailService.Implementation.HtmlEmailSender
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/EmailService/Implementation/HtmlEmailSender.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 21
Coverable lines: 21
Total lines: 41
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
SendAsync()0%620%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/EmailService/Implementation/HtmlEmailSender.cs

#LineLine coverage
 1using System.Net;
 2using System.Net.Mail;
 3using Microsoft.Extensions.Logging;
 4using Microsoft.Extensions.Options;
 5using SlqStudio.Application.Services.EmailService.Models;
 6
 7namespace SlqStudio.Application.Services.EmailService.Implementation;
 8
 09public sealed class HtmlEmailSender(IOptions<SmtpSettings> options, ILogger<HtmlEmailSender> logger) : IHtmlEmailSender
 10{
 11    public async Task<bool> SendAsync(string toAddress, string subject, string htmlBody, CancellationToken ct = default)
 12    {
 013        var smtpSettings = options.Value;
 14        try
 15        {
 016            using var message = new MailMessage(
 017                new MailAddress(smtpSettings.SenderEmail ?? string.Empty, smtpSettings.SenderName),
 018                new MailAddress(toAddress))
 019            {
 020                Subject = subject,
 021                Body = htmlBody,
 022                IsBodyHtml = true
 023            };
 24
 025            using var smtp = new SmtpClient(smtpSettings.Server, smtpSettings.Port)
 026            {
 027                Credentials = new NetworkCredential(smtpSettings.SenderEmail, smtpSettings.Password),
 028                EnableSsl = smtpSettings.EnableSsl
 029            };
 30
 031            await smtp.SendMailAsync(message, ct);
 032            return true;
 33        }
 034        catch (Exception ex)
 35        {
 36            // Адрес получателя в журнал не пишем: достаточно причины сбоя.
 037            logger.LogWarning(ex, "Не удалось отправить письмо через SMTP");
 038            return false;
 39        }
 040    }
 41}