< Summary - SonghayCore

Information
Class: Songhay.Net.SmtpUtility
Assembly: SonghayCore
File(s): /home/rasx/sourceRoot/SonghayCore/SonghayCore/Net/SmtpUtility.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 31
Coverable lines: 31
Total lines: 102
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 12
Branch coverage: 0%
Method coverage

Method coverage is only available for sponsors.

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
GetAttachment(...)0%20%
GetAttachment(...)0%20%
GetMailMessage(...)100%10%
GetMailMessage(...)0%20%
GetMailMessage(...)100%10%
GetMailMessage(...)0%60%

File(s)

/home/rasx/sourceRoot/SonghayCore/SonghayCore/Net/SmtpUtility.cs

#LineLine coverage
 1using System.Net.Mail;
 2
 3namespace Songhay.Net;
 4
 5/// <summary>
 6/// SMTP Utility
 7/// </summary>
 8public static class SmtpUtility
 9{
 10    /// <summary>
 11    /// Gets the attachment.
 12    /// </summary>
 13    /// <param name="path">The path.</param>
 14    public static Attachment GetAttachment(string? path)
 015    {
 016        if (!File.Exists(path)) throw new FileNotFoundException($"“{path}” was not found.");
 17
 018        var attachment = new Attachment(path);
 019        return attachment;
 020    }
 21
 22    /// <summary>
 23    /// Gets the attachment.
 24    /// </summary>
 25    /// <param name="paths">The paths.</param>
 26    public static ICollection<Attachment> GetAttachment(IEnumerable<string>? paths) =>
 027        paths == null
 028            ? Enumerable.Empty<Attachment>().ToList()
 029            : paths.Select(GetAttachment).ToList();
 30
 31    /// <summary>
 32    /// Returns <see cref="MailMessage" />
 33    /// for an instance of <see cref="SmtpClient" />
 34    /// </summary>
 35    /// <param name="from">the from email address</param>
 36    /// <param name="to">the to email address</param>
 37    /// <param name="subject">the email message subject</param>
 38    /// <param name="message">the email message</param>
 39    public static MailMessage GetMailMessage(string from, string to, string subject, string message) =>
 040        GetMailMessage(from, subject, message, new[] {to}, attachments: null);
 41
 42    /// <summary>
 43    /// Returns <see cref="MailMessage" />
 44    /// for an instance of <see cref="SmtpClient" />
 45    /// </summary>
 46    /// <param name="from">the from email address</param>
 47    /// <param name="to">the to email address</param>
 48    /// <param name="subject">the email message subject</param>
 49    /// <param name="message">the email message</param>
 50    /// <param name="attachments">a collection of <see cref="Attachment" /></param>
 51    public static MailMessage GetMailMessage(string from, string to, string subject, string message,
 52        ICollection<Attachment> attachments) =>
 053        !string.IsNullOrWhiteSpace(to)
 054            ? throw new NullReferenceException(nameof(to))
 055            : GetMailMessage(from, subject, message, new[] {to}, attachments);
 56
 57    /// <summary>
 58    /// Returns <see cref="MailMessage" />
 59    /// for an instance of <see cref="SmtpClient" />
 60    /// </summary>
 61    /// <param name="from">the from email address</param>
 62    /// <param name="subject">the email message subject</param>
 63    /// <param name="message">the email message</param>
 64    /// <param name="recipients">a collection of recipients</param>
 65    public static MailMessage GetMailMessage(string from, string subject, string message,
 066        ICollection<string> recipients) => GetMailMessage(from, subject, message, recipients, attachments: null);
 67
 68    /// <summary>
 69    /// Returns <see cref="MailMessage" />
 70    /// for an instance of <see cref="SmtpClient" />
 71    /// </summary>
 72    /// <param name="from">the from email address</param>
 73    /// <param name="subject">the email message subject</param>
 74    /// <param name="message">the email message</param>
 75    /// <param name="recipients">a collection of recipients</param>
 76    /// <param name="attachments">a collection of <see cref="Attachment" /></param>
 77    public static MailMessage GetMailMessage(string? from, string? subject, string? message,
 78        ICollection<string>? recipients, ICollection<Attachment>? attachments)
 079    {
 080        from.ThrowWhenNullOrWhiteSpace();
 081        subject.ThrowWhenNullOrWhiteSpace();
 082        message.ThrowWhenNullOrWhiteSpace();
 083        recipients.ThrowWhenNullOrEmpty();
 84
 085        var msg = new MailMessage
 086        {
 087            From = new MailAddress(from),
 088            Subject = subject,
 089            SubjectEncoding = Encoding.UTF8,
 090            Body = message,
 091            BodyEncoding = Encoding.UTF8,
 092        };
 93
 094        foreach (var recipient in recipients) msg.To.Add(recipient);
 95
 096        if (attachments == null) return msg;
 97
 098        foreach (var i in attachments) msg.Attachments.Add(i);
 99
 0100        return msg;
 0101    }
 102}