|
| 1 | +using System.Net.Mail; |
| 2 | +using Newtonsoft.Json; |
| 3 | + |
| 4 | +namespace CSO.Email |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Mail Message |
| 8 | + /// </summary> |
| 9 | + public class eMail : MailMessage |
| 10 | + { |
| 11 | + #region Properties |
| 12 | + #endregion |
| 13 | + public eMail() |
| 14 | + { |
| 15 | + } |
| 16 | + |
| 17 | + |
| 18 | + #region Methods |
| 19 | + /// <summary> |
| 20 | + /// Send an Email |
| 21 | + /// </summary> |
| 22 | + /// <param name="configDict"></param> |
| 23 | + /// <param name="LogObject"></param> |
| 24 | + /// <returns></returns> |
| 25 | + public bool Send(IDictionary<string, string> configDict, dynamic LogObject) |
| 26 | + { |
| 27 | + /// <summary> |
| 28 | + /// Swtich on/off the service |
| 29 | + /// </summary> |
| 30 | + bool API_EMAIL_ENABLED = Convert.ToBoolean(configDict["API_EMAIL_ENABLED"]); |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// NoReply email address |
| 34 | + /// </summary> |
| 35 | + string API_EMAIL_MAIL_NOREPLY = configDict["API_EMAIL_MAIL_NOREPLY"]; |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Sender email address |
| 39 | + /// </summary> |
| 40 | + string API_EMAIL_MAIL_SENDER = configDict["API_EMAIL_MAIL_SENDER"]; |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Server IP address |
| 44 | + /// </summary> |
| 45 | + string API_EMAIL_SMTP_SERVER = configDict["API_EMAIL_SMTP_SERVER"]; |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Port number |
| 49 | + /// </summary> |
| 50 | + string API_EMAIL_SMTP_PORT = configDict["API_EMAIL_SMTP_PORT"]; |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Flag to indicate if SMTP authentication is required |
| 54 | + /// </summary> |
| 55 | + bool API_EMAIL_SMTP_AUTHENTICATION = Convert.ToBoolean(configDict["API_EMAIL_SMTP_AUTHENTICATION"]); |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Username if authentication is required |
| 59 | + /// </summary> |
| 60 | + string API_EMAIL_SMTP_USERNAME = configDict["API_EMAIL_SMTP_USERNAME"]; |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Password if authentication is required |
| 64 | + /// </summary> |
| 65 | + string API_EMAIL_SMTP_PASSWORD = configDict["API_EMAIL_SMTP_PASSWORD"]; |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Flag to indicate if SSL is required |
| 69 | + /// </summary> |
| 70 | + bool API_EMAIL_SMTP_SSL = Convert.ToBoolean(configDict["API_EMAIL_SMTP_SSL"]); |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Template Datetime Mask |
| 74 | + /// </summary> |
| 75 | + // private readonly string API_EMAIL_DATETIME_MASK = ApiServicesHelper.ApiConfiguration.Settings["API_EMAIL_DATETIME_MASK"]; |
| 76 | + |
| 77 | + LogObject.Info("Email Enabled: " + API_EMAIL_ENABLED); |
| 78 | + LogObject.Info("Email NoReply: " + API_EMAIL_MAIL_NOREPLY); |
| 79 | + LogObject.Info("Email Sender: " + API_EMAIL_MAIL_SENDER); |
| 80 | + LogObject.Info("SMTP Server: " + API_EMAIL_SMTP_SERVER); |
| 81 | + LogObject.Info("SMTP Port: " + API_EMAIL_SMTP_PORT); |
| 82 | + LogObject.Info("SMTP Authentication: " + API_EMAIL_SMTP_AUTHENTICATION); |
| 83 | + LogObject.Info("SMTP Username: " + API_EMAIL_SMTP_USERNAME); |
| 84 | + LogObject.Info("SMTP Password: ********"); // Hide API_EMAIL_SMTP_PAsSSWORD from logs |
| 85 | + LogObject.Info("SMTP SSL: " + API_EMAIL_SMTP_SSL); |
| 86 | + |
| 87 | + if (!API_EMAIL_ENABLED) |
| 88 | + { |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + try |
| 93 | + { |
| 94 | + // Initiate new SMTP Client |
| 95 | + SmtpClient smtpClient = new SmtpClient(API_EMAIL_SMTP_SERVER, Convert.ToInt32(API_EMAIL_SMTP_PORT)); |
| 96 | + smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; |
| 97 | + |
| 98 | + if (API_EMAIL_SMTP_AUTHENTICATION |
| 99 | + && !string.IsNullOrWhiteSpace(API_EMAIL_SMTP_USERNAME) |
| 100 | + && !string.IsNullOrWhiteSpace(API_EMAIL_SMTP_PASSWORD)) |
| 101 | + { |
| 102 | + // Use authentication if any |
| 103 | + smtpClient.Credentials = new System.Net.NetworkCredential(API_EMAIL_SMTP_USERNAME, API_EMAIL_SMTP_PASSWORD); |
| 104 | + smtpClient.UseDefaultCredentials = true; |
| 105 | + } |
| 106 | + |
| 107 | + if (API_EMAIL_SMTP_SSL) |
| 108 | + { |
| 109 | + // Use SSL if any |
| 110 | + smtpClient.EnableSsl = true; |
| 111 | + } |
| 112 | + |
| 113 | + // Override Sender, From, Reply To for security |
| 114 | + this.ReplyToList.Clear(); |
| 115 | + this.ReplyToList.Add(new MailAddress(API_EMAIL_MAIL_NOREPLY)); |
| 116 | + this.From = new MailAddress(API_EMAIL_MAIL_SENDER); |
| 117 | + this.Sender = new MailAddress(API_EMAIL_MAIL_SENDER); |
| 118 | + |
| 119 | + // Set the HTML body |
| 120 | + this.IsBodyHtml = true; |
| 121 | + |
| 122 | + // Send the mail |
| 123 | + smtpClient.Send(this); |
| 124 | + |
| 125 | + LogObject.Info("eMail sent"); |
| 126 | + return true; |
| 127 | + } |
| 128 | + catch (Exception e) |
| 129 | + { |
| 130 | + LogObject.Fatal(e); |
| 131 | + return false; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /// <summary> |
| 136 | + /// Parse a Template located in Properties.Resources |
| 137 | + /// </summary> |
| 138 | + /// <param name="template"></param> |
| 139 | + /// <param name="eMail_KeyValuePair"></param> |
| 140 | + /// <param name="LogObject"></param> |
| 141 | + /// <returns></returns> |
| 142 | + public string ParseTemplate(string template, List<eMail_KeyValuePair> eMail_KeyValuePair, dynamic LogObject) |
| 143 | + { |
| 144 | + |
| 145 | + LogObject.Info("eMail String-Template to parse: " + template); |
| 146 | + LogObject.Info("eMail List to parse: " + JsonConvert.SerializeObject(eMail_KeyValuePair, Formatting.None, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.None, ReferenceLoopHandling = ReferenceLoopHandling.Ignore })); |
| 147 | + try |
| 148 | + { |
| 149 | + // Parse nodes |
| 150 | + foreach (var item in eMail_KeyValuePair) |
| 151 | + { |
| 152 | + template = template.Replace(item.key, item.value); |
| 153 | + } |
| 154 | + |
| 155 | + return template; |
| 156 | + |
| 157 | + } |
| 158 | + catch (Exception e) |
| 159 | + { |
| 160 | + LogObject.Fatal(e); |
| 161 | + throw; |
| 162 | + } |
| 163 | + } |
| 164 | + #endregion |
| 165 | + } |
| 166 | + |
| 167 | + public class eMail_KeyValuePair |
| 168 | + { |
| 169 | + #region Properties |
| 170 | + /// <summary> |
| 171 | + /// Key to parse |
| 172 | + /// </summary> |
| 173 | + public string key { get; set; } |
| 174 | + |
| 175 | + /// <summary> |
| 176 | + /// Value to parse |
| 177 | + /// </summary> |
| 178 | + public string value { get; set; } |
| 179 | + |
| 180 | + #endregion |
| 181 | + |
| 182 | + /// <summary> |
| 183 | + /// Initialise a blank one |
| 184 | + /// </summary> |
| 185 | + public eMail_KeyValuePair() |
| 186 | + { |
| 187 | + key = null; |
| 188 | + value = null; |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | +} |
0 commit comments