| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="MailToHelper.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Linq; |
| | | 10 | | using MyNet.Primitives.Helpers; |
| | | 11 | | |
| | | 12 | | namespace 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> |
| | | 17 | | public 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> |
| | 0 | 26 | | 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 | | { |
| | 6 | 37 | | var recipients = addresses |
| | 6 | 38 | | .Where(x => !string.IsNullOrWhiteSpace(x)) |
| | 6 | 39 | | .Select(x => x.Trim()) |
| | 6 | 40 | | .ToArray(); |
| | | 41 | | |
| | 6 | 42 | | if (recipients.Length == 0) |
| | | 43 | | { |
| | 6 | 44 | | return false; |
| | | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | var values = new Dictionary<string, string>(); |
| | 0 | 48 | | if (!string.IsNullOrEmpty(title)) |
| | | 49 | | { |
| | 0 | 50 | | values.Add("subject", title); |
| | | 51 | | } |
| | | 52 | | |
| | 0 | 53 | | if (!string.IsNullOrEmpty(body)) |
| | | 54 | | { |
| | 0 | 55 | | values.Add("body", body); |
| | | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | var command = $"mailto:{string.Join(";", recipients)}"; |
| | | 59 | | |
| | 0 | 60 | | if (values.Count != 0) |
| | | 61 | | { |
| | 0 | 62 | | command += $"?{string.Join("&", values.Select(x => $"{x.Key}={Uri.EscapeDataString(x.Value)}"))}"; |
| | | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | ProcessHelper.Start(command); |
| | 0 | 66 | | return true; |
| | | 67 | | } |
| | | 68 | | } |
| | | 69 | | |