| | 1 | | using System.Net.Mail; |
| | 2 | |
|
| | 3 | | namespace Songhay.Net; |
| | 4 | |
|
| | 5 | | /// <summary> |
| | 6 | | /// SMTP Utility |
| | 7 | | /// </summary> |
| | 8 | | public 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) |
| 0 | 15 | | { |
| 0 | 16 | | if (!File.Exists(path)) throw new FileNotFoundException($"“{path}” was not found."); |
| | 17 | |
|
| 0 | 18 | | var attachment = new Attachment(path); |
| 0 | 19 | | return attachment; |
| 0 | 20 | | } |
| | 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) => |
| 0 | 27 | | paths == null |
| 0 | 28 | | ? Enumerable.Empty<Attachment>().ToList() |
| 0 | 29 | | : 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) => |
| 0 | 40 | | 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) => |
| 0 | 53 | | !string.IsNullOrWhiteSpace(to) |
| 0 | 54 | | ? throw new NullReferenceException(nameof(to)) |
| 0 | 55 | | : 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, |
| 0 | 66 | | 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) |
| 0 | 79 | | { |
| 0 | 80 | | from.ThrowWhenNullOrWhiteSpace(); |
| 0 | 81 | | subject.ThrowWhenNullOrWhiteSpace(); |
| 0 | 82 | | message.ThrowWhenNullOrWhiteSpace(); |
| 0 | 83 | | recipients.ThrowWhenNullOrEmpty(); |
| | 84 | |
|
| 0 | 85 | | var msg = new MailMessage |
| 0 | 86 | | { |
| 0 | 87 | | From = new MailAddress(from), |
| 0 | 88 | | Subject = subject, |
| 0 | 89 | | SubjectEncoding = Encoding.UTF8, |
| 0 | 90 | | Body = message, |
| 0 | 91 | | BodyEncoding = Encoding.UTF8, |
| 0 | 92 | | }; |
| | 93 | |
|
| 0 | 94 | | foreach (var recipient in recipients) msg.To.Add(recipient); |
| | 95 | |
|
| 0 | 96 | | if (attachments == null) return msg; |
| | 97 | |
|
| 0 | 98 | | foreach (var i in attachments) msg.Attachments.Add(i); |
| | 99 | |
|
| 0 | 100 | | return msg; |
| 0 | 101 | | } |
| | 102 | | } |