1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| public class MailNotificationConfiguration { public string SMTP { get; set; } public string[] Notify { get; set; } public string[] EmailWhiteList { get; set; } }
public interface INotificationService { Task NotifyByMailAsync(string subject, string body, string[] to, object att = null, string[] cc = null);
string MakeEmailBody(string name, params string[] contentLines); }
public class NotificationService : INotificationService { private readonly MailNotificationConfiguration _notificationConfig; private readonly string _env; public NotificationService(MailNotificationConfiguration config, string env) { _notificationConfig = config; _env = env; }
public string MakeEmailBody(string name, params string[] contentLines) { var contentSb = new StringBuilder("<html>"); contentSb.Append("<body>"); contentSb.Append($"<p>Hi {name}, </p>"); contentSb.Append("<div>"); if (contentLines != null && contentLines.Count() > 0) { foreach (var item in contentLines) { contentSb.AppendLine($"<p>{item}</p>"); } } contentSb.Append("<div>"); contentSb.Append("<br/>"); contentSb.Append("<div>"); contentSb.Append("<p>Thanks & Regards</p>"); contentSb.Append("<p>XXXX</p>"); contentSb.Append("<p><b style='background-color:yellow'>Note: This mail is sent automatically, please do not reply.</b></p>"); contentSb.Append("</div>"); contentSb.Append("</body>"); contentSb.Append("</html>");
return contentSb.ToString(); }
public async Task NotifyByMailAsync(string subject, string body, string[] to, object att = null, string[] cc = null) { var mail = new MailMessage() { From = new MailAddress("SENDING_EMAIL_NAME@XXXX.com", "EMAIL_DISPLAY_NAME"), BodyEncoding = Encoding.UTF8, IsBodyHtml = true, DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure, Subject = IsProductEnvironment() ? subject : $"[{_env}] {subject}", Body = body };
var inMailAttachment = att as Attachment;
if (inMailAttachment != null) { mail.Attachments.Add(inMailAttachment); } to.ToList().ForEach(n => mail.To.Add(n));
cc?.ToList().ForEach(ccAddress => mail.CC.Add(ccAddress));
try { using var client = new SmtpClient { Host = _notificationConfig.SMTP, DeliveryMethod = SmtpDeliveryMethod.Network };
if (IsProductEnvironment()) { await client.SendMailAsync(mail); } else { if (mail.To.Count() > 0) await client.SendMailAsync(mail); } } catch (Exception ex) { ; } finally { if (inMailAttachment != null) inMailAttachment.Dispose(); } }
private bool IsProductEnvironment() { return _env == "Production" || _env == "Stage"; } }
|