< Summary

Information
Class: MyNet.Mail.MailToHelper
Assembly: MyNet.Mail
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Mail/MailToHelper.cs
Tag: 342_31701152804
Line coverage
35%
Covered lines: 6
Uncovered lines: 11
Coverable lines: 17
Total lines: 69
Line coverage: 35.2%
Branch coverage
12%
Covered branches: 1
Total branches: 8
Branch coverage: 12.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SendMail(...)100%210%
SendMail(...)12.5%24837.5%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Mail/MailToHelper.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="MailToHelper.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using System.Linq;
 10using MyNet.Primitives.Helpers;
 11
 12namespace MyNet.Mail;
 13
 14/// <summary>
 15/// Helper class for sending emails using the default mail client on the user's system. This class provides methods to c
 16/// </summary>
 17public static class MailToHelper
 18{
 19    /// <summary>
 20    /// Sends an email to a single recipient using the default mail client on the user's system. This method is a conven
 21    /// </summary>
 22    /// <param name="address">The email address of the recipient.</param>
 23    /// <param name="title">The subject of the email.</param>
 24    /// <param name="body">The body of the email.</param>
 25    /// <returns>True if the email was successfully sent, otherwise false.</returns>
 026    public static bool SendMail(string address, string title, string body) => SendMail([address], title, body);
 27
 28    /// <summary>
 29    /// Sends an email using the default mail client on the user's system. This method constructs a "mailto:" URI with t
 30    /// </summary>
 31    /// <param name="addresses">The email addresses of the recipients.</param>
 32    /// <param name="title">The subject of the email.</param>
 33    /// <param name="body">The body of the email.</param>
 34    /// <returns>True if the email was successfully sent, otherwise false.</returns>
 35    public static bool SendMail(IEnumerable<string> addresses, string title, string body)
 36    {
 637        var recipients = addresses
 638            .Where(x => !string.IsNullOrWhiteSpace(x))
 639            .Select(x => x.Trim())
 640            .ToArray();
 41
 642        if (recipients.Length == 0)
 43        {
 644            return false;
 45        }
 46
 047        var values = new Dictionary<string, string>();
 048        if (!string.IsNullOrEmpty(title))
 49        {
 050            values.Add("subject", title);
 51        }
 52
 053        if (!string.IsNullOrEmpty(body))
 54        {
 055            values.Add("body", body);
 56        }
 57
 058        var command = $"mailto:{string.Join(";", recipients)}";
 59
 060        if (values.Count != 0)
 61        {
 062            command += $"?{string.Join("&", values.Select(x => $"{x.Key}={Uri.EscapeDataString(x.Value)}"))}";
 63        }
 64
 065        ProcessHelper.Start(command);
 066        return true;
 67    }
 68}
 69