< Summary

Information
Class: SlqStudio.Application.Services.EmailService.Implementation.EmailService
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/EmailService/Implementation/EmailService.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 45
Coverable lines: 45
Total lines: 89
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
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%
SendEmailAsync()0%2040%
SendEmailWithAttachmentAsync()0%620%

File(s)

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

#LineLine coverage
 1using System.Net;
 2using System.Net.Mail;
 3using Microsoft.Extensions.Options;
 4using SlqStudio.Application.Services.EmailService.Models;
 5
 6namespace SlqStudio.Application.Services.EmailService.Implementation;
 7
 8public class EmailService : IEmailService
 9{
 10    private readonly SmtpSettings _smtpSettings;
 11
 012    public EmailService(IOptions<SmtpSettings> smtpSettings)
 13    {
 014        _smtpSettings = smtpSettings.Value;
 015    }
 16
 17    public async Task<bool> SendEmailAsync(string subject, string body)
 18    {
 19        try
 20        {
 021            var fromAddress = new MailAddress(_smtpSettings.SenderEmail ?? "", _smtpSettings.SenderName);
 022            var toAddress = new MailAddress(_smtpSettings.RecipientEmail ?? "");
 23
 024            var htmlBody = System.Net.WebUtility.HtmlEncode(body)
 025                .Replace("\r\n", "<br>")
 026                .Replace("\n", "<br>");
 27
 028            using var message = new MailMessage(fromAddress, toAddress)
 029            {
 030                Subject = subject,
 031                Body = htmlBody,
 032                IsBodyHtml = true
 033            };
 34
 035            using var smtp = new SmtpClient(_smtpSettings.Server, _smtpSettings.Port)
 036            {
 037                Credentials = new NetworkCredential(_smtpSettings.SenderEmail, _smtpSettings.Password),
 038                EnableSsl = _smtpSettings.EnableSsl
 039            };
 40
 041            await smtp.SendMailAsync(message);
 042            return true;
 43        }
 044        catch
 45        {
 046            return false;
 47        }
 048    }
 49
 50    public async Task<bool> SendEmailWithAttachmentAsync(
 51        string toAddress,
 52        string subject,
 53        string body,
 54        byte[] attachment,
 55        string attachmentName,
 56        string attachmentMimeType = "application/octet-stream")
 57    {
 58        try
 59        {
 060            var fromAddress = new MailAddress(_smtpSettings.SenderEmail ?? "", _smtpSettings.SenderName);
 061            var to = new MailAddress(toAddress);
 62
 063            using var message = new MailMessage(fromAddress, to)
 064            {
 065                Subject = subject,
 066                Body = body,
 067                IsBodyHtml = false
 068            };
 69
 070            using var ms = new MemoryStream(attachment);
 071            var ct = new System.Net.Mime.ContentType(attachmentMimeType) { Name = attachmentName };
 072            message.Attachments.Add(new Attachment(ms, ct));
 73
 074            using var smtp = new SmtpClient(_smtpSettings.Server, _smtpSettings.Port)
 075            {
 076                Credentials = new NetworkCredential(_smtpSettings.SenderEmail, _smtpSettings.Password),
 077                EnableSsl = _smtpSettings.EnableSsl
 078            };
 79
 080            await smtp.SendMailAsync(message);
 081            return true;
 82        }
 083        catch
 84        {
 085            return false;
 86        }
 087    }
 88
 89}