|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace Email4 |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Methods for generating an email. By Jeremy Coulson. |
| 10 | + /// </summary> |
| 11 | + public class Generation |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Sends an email with more options. |
| 15 | + /// </summary> |
| 16 | + /// <param name="strFrom">The from field. Required.</param> |
| 17 | + /// <param name="strTo">Recipients. Separate with comma. At least one is required.</param> |
| 18 | + /// <param name="strCC">CC recipients. Separate with comma. Optional; use null or Nothing if blank.</param> |
| 19 | + /// <param name="strAttachments">Attachments. Separate with comma. Optional; use null or Nothing if blank.</param> |
| 20 | + /// <param name="strSubject">The subject of the message. Required.</param> |
| 21 | + /// <param name="strBody">The body of the message. Required.</param> |
| 22 | + /// <param name="strReplyTo">Reply recipient. Only list one. On IIS 7, this requires integreated pipeline mode. Optional; use null or Nothing if blank.</param> |
| 23 | + /// <param name="boolBodyHtml">Is the body HTML or not? Required.</param> |
| 24 | + /// <param name="strMailServer">The SMTP client to send this message. Required.</param> |
| 25 | + /// <param name="strUser">The SMTP user. Required.</param> |
| 26 | + /// <param name="strPass">The SMPT password. Required.</param> |
| 27 | + /// <param name="boolSsl">True to connect with SSL. False to connect without SSL. Required.</param> |
| 28 | + /// <returns>Retuns success or exception.</returns> |
| 29 | + public string SendEmail(string strFrom, string strTo, string strCC, string strAttachments, string strSubject, string strBody, string strReplyTo, bool boolBodyHtml, string strMailServer, string strUser, string strPass, bool boolSsl) |
| 30 | + { |
| 31 | + // String to hold result message. |
| 32 | + string strEmailSuccess = String.Empty; |
| 33 | + |
| 34 | + try |
| 35 | + { |
| 36 | + using (System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage()) |
| 37 | + { |
| 38 | + msg.IsBodyHtml = boolBodyHtml; |
| 39 | + msg.From = new System.Net.Mail.MailAddress(strFrom); |
| 40 | + // Split the To string into an array and add each address. |
| 41 | + string[] strToArray = strTo.Split(','); |
| 42 | + foreach (string strToRecipient in strToArray) |
| 43 | + { |
| 44 | + msg.To.Add(new System.Net.Mail.MailAddress(strToRecipient)); |
| 45 | + } |
| 46 | + if ((strCC != null) && (strCC != String.Empty) && (strCC.Length > 0)) |
| 47 | + { |
| 48 | + // Split the CC string into an array and add each address. |
| 49 | + string[] strCCArray = strCC.Split(','); |
| 50 | + foreach (string strCCRecipient in strCCArray) |
| 51 | + { |
| 52 | + msg.CC.Add(new System.Net.Mail.MailAddress(strCCRecipient)); |
| 53 | + } |
| 54 | + } |
| 55 | + msg.Subject = strSubject; |
| 56 | + msg.Body = strBody; |
| 57 | + if ((strReplyTo != null) && (strReplyTo != String.Empty) && (strReplyTo.Length > 0)) |
| 58 | + { |
| 59 | + msg.ReplyTo = new System.Net.Mail.MailAddress(strReplyTo); |
| 60 | + } |
| 61 | + if ((strAttachments != null) && (strAttachments != String.Empty) && (strAttachments.Length > 0)) |
| 62 | + { |
| 63 | + // Split the attachments string into an array and add each attachment. |
| 64 | + string[] strAttachmentArray = strAttachments.Split(','); |
| 65 | + foreach (string strAttachment in strAttachmentArray) |
| 66 | + { |
| 67 | + System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(strAttachment); |
| 68 | + msg.Attachments.Add(attachment); |
| 69 | + } |
| 70 | + } |
| 71 | + using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(strMailServer)) |
| 72 | + { |
| 73 | + System.Net.NetworkCredential user = new System.Net.NetworkCredential(strUser, strPass); |
| 74 | + client.Credentials = user; |
| 75 | + client.EnableSsl = boolSsl; |
| 76 | + client.Send(msg); |
| 77 | + } |
| 78 | + } |
| 79 | + // Success! Say so. |
| 80 | + strEmailSuccess = "Complex overload sent at " + System.DateTime.Now.ToString() + "!"; |
| 81 | + } |
| 82 | + catch (Exception ex) |
| 83 | + { |
| 84 | + // Problem! Say so. |
| 85 | + strEmailSuccess = ex.ToString(); |
| 86 | + } |
| 87 | + |
| 88 | + return strEmailSuccess; |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Sends an email with fewer options. SMTP server info comes from Web.config. |
| 93 | + /// </summary> |
| 94 | + /// <param name="strFrom">The from field. Required.</param> |
| 95 | + /// <param name="strTo">Recipients. Separate with comma. Required.</param> |
| 96 | + /// <param name="strCC">CC recipients. Separate with comma. Optional; use null or Nothing if blank.</param> |
| 97 | + /// <param name="strAttachments">Attachments. Separate with comma. Optional; use null or Nothing if blank.</param> |
| 98 | + /// <param name="strSubject">The subject of the message. Required.</param> |
| 99 | + /// <param name="strBody">The body of the message. Required.</param> |
| 100 | + /// <returns>Retuns success or exception.</returns> |
| 101 | + public string SendEmail(string strFrom, string strTo, string strCC, string strAttachments, string strSubject, string strBody) |
| 102 | + { |
| 103 | + // Create string to hold success message. |
| 104 | + string strEmailSuccess = String.Empty; |
| 105 | + |
| 106 | + try |
| 107 | + { |
| 108 | + using (System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage()) |
| 109 | + { |
| 110 | + |
| 111 | + msg.IsBodyHtml = false; |
| 112 | + msg.From = new System.Net.Mail.MailAddress(strFrom); |
| 113 | + // Split the To string into an array and add each address. |
| 114 | + string[] strToArray = strTo.Split(','); |
| 115 | + foreach (string strToRecipient in strToArray) |
| 116 | + { |
| 117 | + msg.To.Add(new System.Net.Mail.MailAddress(strToRecipient)); |
| 118 | + } |
| 119 | + if ((strCC != null) && (strCC != String.Empty) && (strCC.Length > 0)) |
| 120 | + { |
| 121 | + // Split the CC string into an array and add each address. |
| 122 | + string[] strCCArray = strCC.Split(','); |
| 123 | + foreach (string strCCRecipient in strCCArray) |
| 124 | + { |
| 125 | + msg.CC.Add(new System.Net.Mail.MailAddress(strCCRecipient)); |
| 126 | + } |
| 127 | + } |
| 128 | + msg.Subject = strSubject; |
| 129 | + msg.Body = strBody; |
| 130 | + if ((strAttachments != null) && (strAttachments != String.Empty) && (strAttachments.Length > 0)) |
| 131 | + { |
| 132 | + // Split the attachments string into an array and add each attachment. |
| 133 | + string[] strAttachmentArray = strAttachments.Split(','); |
| 134 | + foreach (string strAttachment in strAttachmentArray) |
| 135 | + { |
| 136 | + System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(strAttachment); |
| 137 | + msg.Attachments.Add(attachment); |
| 138 | + } |
| 139 | + } |
| 140 | + using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(System.Web.Configuration.WebConfigurationManager.AppSettings["GlobalSMTP"].ToString())) |
| 141 | + { |
| 142 | + System.Net.NetworkCredential user = new System.Net.NetworkCredential(System.Web.Configuration.WebConfigurationManager.AppSettings["GlobalSMTPUser"].ToString(), System.Web.Configuration.WebConfigurationManager.AppSettings["GlobalSMTPPass"].ToString()); |
| 143 | + client.Credentials = user; |
| 144 | + client.EnableSsl = true; |
| 145 | + client.Send(msg); |
| 146 | + } |
| 147 | + strEmailSuccess = "Simple overload sent at " + System.DateTime.Now.ToString() + "!"; |
| 148 | + } |
| 149 | + } |
| 150 | + catch (Exception ex) |
| 151 | + { |
| 152 | + strEmailSuccess = ex.ToString(); |
| 153 | + } |
| 154 | + |
| 155 | + return strEmailSuccess; |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments