| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="Email.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.IO; |
| | | 10 | | using System.Linq; |
| | | 11 | | using MyNet.Mail.Models; |
| | | 12 | | |
| | | 13 | | namespace MyNet.Mail; |
| | | 14 | | |
| | | 15 | | public class Email(string fromAddress, string displayNameFrom = "") : IEmail |
| | | 16 | | { |
| | 108 | 17 | | public EmailData Data { get; } = new(new EmailAddress(fromAddress, displayNameFrom)); |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Creates a new Email instance and sets the from property. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="emailAddress">Email address to send from.</param> |
| | | 23 | | /// <param name="name">Name to send from.</param> |
| | | 24 | | /// <returns>Instance of the Email class.</returns> |
| | 108 | 25 | | public static IEmail From(string emailAddress, string name = "") => new Email(emailAddress, name); |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Set the send from email address. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="emailAddress">Email address of sender.</param> |
| | | 31 | | /// <param name="name">Name of sender.</param> |
| | | 32 | | /// <returns>Instance of the Email class.</returns> |
| | | 33 | | public IEmail SetFrom(string emailAddress, string name = "") |
| | | 34 | | { |
| | 3 | 35 | | Data.From = new EmailAddress(emailAddress, name); |
| | 3 | 36 | | return this; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Adds a recipients to the email, Splits name and address on ';'. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="emailAddress">Email address of recipients.</param> |
| | | 43 | | /// <param name="name">Name of recipient.</param> |
| | | 44 | | /// <returns>Instance of the Email class.</returns> |
| | 75 | 45 | | public IEmail To(string emailAddress, string name = "") => AddAddresses(Data.To, emailAddress, name); |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Adds all recipients in list to email. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="mailAddresses">List of recipients.</param> |
| | | 51 | | /// <returns>Instance of the Email class.</returns> |
| | 3 | 52 | | public IEmail To(IList<EmailAddress> mailAddresses) => AddAddresses(Data.To, mailAddresses); |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Adds a Carbon Copy to the email. |
| | | 56 | | /// </summary> |
| | | 57 | | /// <param name="emailAddress">Email address of recipients.</param> |
| | | 58 | | /// <param name="name">Name of recipient.</param> |
| | | 59 | | /// <returns>Instance of the Email class.</returns> |
| | 9 | 60 | | public IEmail Cc(string emailAddress, string name = "") => AddAddresses(Data.Cc, emailAddress, name); |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Adds a Carbon Copy to the email. |
| | | 64 | | /// </summary> |
| | | 65 | | /// <param name="mailAddresses">List of recipients.</param> |
| | | 66 | | /// <returns>Instance of the Email class.</returns> |
| | 0 | 67 | | public IEmail Cc(IList<EmailAddress> mailAddresses) => AddAddresses(Data.Cc, mailAddresses); |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Adds a blind carbon copy to the email. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <param name="emailAddress">Email address of recipient.</param> |
| | | 73 | | /// <param name="name">Name of recipient.</param> |
| | | 74 | | /// <returns>Instance of the Email class.</returns> |
| | 9 | 75 | | public IEmail Bcc(string emailAddress, string name = "") => AddAddresses(Data.Bcc, emailAddress, name); |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Adds a blind carbon copy to the email. |
| | | 79 | | /// </summary> |
| | | 80 | | /// <param name="mailAddresses">List of recipients.</param> |
| | | 81 | | /// <returns>Instance of the Email class.</returns> |
| | 0 | 82 | | public IEmail Bcc(IList<EmailAddress> mailAddresses) => AddAddresses(Data.Bcc, mailAddresses); |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Sets the ReplyTo address on the email. |
| | | 86 | | /// </summary> |
| | | 87 | | /// <param name="emailAddress">Email address of recipient.</param> |
| | | 88 | | /// <param name="name">Name of recipient.</param> |
| | | 89 | | /// <returns>Instance of the Email class.</returns> |
| | 9 | 90 | | public IEmail ReplyTo(string emailAddress, string name = "") => AddAddresses(Data.ReplyTo, emailAddress, name); |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Sets the ReplyTo address on the email. |
| | | 94 | | /// </summary> |
| | | 95 | | /// <param name="mailAddresses">List of recipients.</param> |
| | | 96 | | /// <returns>Instance of the Email class.</returns> |
| | 0 | 97 | | public IEmail ReplyTo(IList<EmailAddress> mailAddresses) => AddAddresses(Data.ReplyTo, mailAddresses); |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Sets the subject of the email. |
| | | 101 | | /// </summary> |
| | | 102 | | /// <param name="subject">email subject.</param> |
| | | 103 | | /// <returns>Instance of the Email class.</returns> |
| | | 104 | | public IEmail Subject(string subject) |
| | | 105 | | { |
| | 21 | 106 | | Data.Subject = subject; |
| | 21 | 107 | | return this; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Adds a Body to the Email. |
| | | 112 | | /// </summary> |
| | | 113 | | /// <param name="body">The content of the body.</param> |
| | | 114 | | /// <param name="isHtml">True if Body is HTML, false for plain text (default).</param> |
| | | 115 | | public IEmail Body(string body, bool isHtml = false) |
| | | 116 | | { |
| | 27 | 117 | | Data.IsHtml = isHtml; |
| | 27 | 118 | | Data.Body = body; |
| | 27 | 119 | | return this; |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Adds a Plaintext alternative Body to the Email. Used in conjunction with an HTML email, |
| | | 124 | | /// this allows for email readers without html capability, and also helps avoid spam filters. |
| | | 125 | | /// </summary> |
| | | 126 | | /// <param name="body">The content of the body.</param> |
| | | 127 | | public IEmail PlaintextAlternativeBody(string body) |
| | | 128 | | { |
| | 9 | 129 | | Data.PlaintextAlternativeBody = body; |
| | 9 | 130 | | return this; |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Marks the email as High Priority. |
| | | 135 | | /// </summary> |
| | | 136 | | public IEmail HighPriority() |
| | | 137 | | { |
| | 9 | 138 | | Data.Priority = Priority.High; |
| | 9 | 139 | | return this; |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Marks the email as Low Priority. |
| | | 144 | | /// </summary> |
| | | 145 | | public IEmail LowPriority() |
| | | 146 | | { |
| | 9 | 147 | | Data.Priority = Priority.Low; |
| | 9 | 148 | | return this; |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | /// <summary> |
| | | 152 | | /// Adds an Attachment to the Email. |
| | | 153 | | /// </summary> |
| | | 154 | | /// <param name="attachment">The Attachment to add.</param> |
| | | 155 | | /// <returns>Instance of the Email class.</returns> |
| | | 156 | | public IEmail Attach(Attachment attachment) |
| | | 157 | | { |
| | 15 | 158 | | if (!Data.Attachments.Contains(attachment)) |
| | 12 | 159 | | Data.Attachments.Add(attachment); |
| | | 160 | | |
| | 15 | 161 | | return this; |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | /// <summary> |
| | | 165 | | /// Adds Multiple Attachments to the Email. |
| | | 166 | | /// </summary> |
| | | 167 | | /// <param name="attachments">The List of Attachments to add.</param> |
| | | 168 | | /// <returns>Instance of the Email class.</returns> |
| | | 169 | | public IEmail Attach(IList<Attachment> attachments) |
| | | 170 | | { |
| | 18 | 171 | | foreach (var attachment in attachments.Where(attachment => !Data.Attachments.Contains(attachment))) |
| | 6 | 172 | | Data.Attachments.Add(attachment); |
| | | 173 | | |
| | 3 | 174 | | return this; |
| | | 175 | | } |
| | | 176 | | |
| | | 177 | | public IEmail AttachFromFilename(string filename, string contentType = "", string attachmentName = "") |
| | | 178 | | { |
| | 6 | 179 | | if (string.IsNullOrWhiteSpace(filename)) throw new ArgumentException("Filename must be provided.", nameof(filena |
| | | 180 | | |
| | 0 | 181 | | var stream = File.OpenRead(filename); |
| | 0 | 182 | | _ = Attach(new Attachment |
| | 0 | 183 | | { |
| | 0 | 184 | | Data = stream, |
| | 0 | 185 | | Filename = !string.IsNullOrWhiteSpace(attachmentName) ? attachmentName : Path.GetFileName(filename), |
| | 0 | 186 | | ContentType = contentType |
| | 0 | 187 | | }); |
| | | 188 | | |
| | 0 | 189 | | return this; |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | public IEmail Header(string header, string body) |
| | | 193 | | { |
| | 12 | 194 | | if (!string.IsNullOrWhiteSpace(header)) |
| | 9 | 195 | | Data.Headers[header] = body; |
| | | 196 | | |
| | 12 | 197 | | return this; |
| | | 198 | | } |
| | | 199 | | |
| | 3 | 200 | | public override string ToString() => $"From {Data.From.Address} To {string.Join(", ", Data.To.Select(x => x.Address) |
| | | 201 | | |
| | | 202 | | /// <summary> |
| | | 203 | | /// Adds a recipient to the email, Splits name and address on ';'. |
| | | 204 | | /// </summary> |
| | | 205 | | /// <returns>Instance of the Email class.</returns> |
| | | 206 | | private Email AddAddresses(IList<EmailAddress> destination, string emailAddress, string name) |
| | | 207 | | { |
| | 105 | 208 | | if (string.IsNullOrWhiteSpace(emailAddress)) return this; |
| | | 209 | | |
| | 99 | 210 | | if (emailAddress.Contains(';', StringComparison.OrdinalIgnoreCase)) |
| | | 211 | | { |
| | | 212 | | // email address has semicolon, try split |
| | 3 | 213 | | var nameSplit = name.Split(';', StringSplitOptions.TrimEntries); |
| | 3 | 214 | | var addressSplit = emailAddress.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEn |
| | 18 | 215 | | for (var i = 0; i < addressSplit.Length; i++) |
| | | 216 | | { |
| | 6 | 217 | | var currentName = string.Empty; |
| | 6 | 218 | | if (nameSplit.Length - 1 >= i) |
| | 6 | 219 | | currentName = nameSplit[i]; |
| | | 220 | | |
| | 6 | 221 | | if (!string.IsNullOrWhiteSpace(addressSplit[i])) |
| | 6 | 222 | | destination.Add(new EmailAddress(addressSplit[i], currentName.Trim())); |
| | | 223 | | } |
| | | 224 | | } |
| | | 225 | | else |
| | | 226 | | { |
| | 96 | 227 | | destination.Add(new EmailAddress(emailAddress.Trim(), name.Trim())); |
| | | 228 | | } |
| | | 229 | | |
| | 99 | 230 | | return this; |
| | | 231 | | } |
| | | 232 | | |
| | | 233 | | /// <summary> |
| | | 234 | | /// Adds all recipients in list to email. |
| | | 235 | | /// </summary> |
| | | 236 | | /// <returns>Instance of the Email class.</returns> |
| | | 237 | | private Email AddAddresses(IList<EmailAddress> addresses, IList<EmailAddress> mailAddresses) |
| | | 238 | | { |
| | 18 | 239 | | foreach (var address in mailAddresses.Where(x => !string.IsNullOrWhiteSpace(x.Address))) |
| | | 240 | | { |
| | 6 | 241 | | addresses.Add(address); |
| | | 242 | | } |
| | | 243 | | |
| | 3 | 244 | | return this; |
| | | 245 | | } |
| | | 246 | | } |
| | | 247 | | |