//****************************************************************************************************** // Mail.cs - Gbtc // // Copyright © 2012, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 12/30/2005 - Pinal C. Patel // Generated original version of source code. // 12/12/2007 - Darrell Zuercher // Edited Code Comments. // 09/22/2008 - J. Ritchie Carroll // Converted to C# - restructured. // 10/15/2008 - Pinal C. Patel // Edited code comments. // 09/14/2009 - Stephen C. Wills // Added new header and license agreement. // 09/02/2010 - Pinal C. Patel // Exposed underlying SmtpClient object via Client property for greater control. // 12/01/2010 - Pinal C. Patel // Updated Send() method to parse To property value that could contain multiple email addresses. // 12/14/2012 - Starlynn Danyelle Gilliam // Modified Header. // //****************************************************************************************************** using System; using System.IO; using System.Net; using System.Net.Mail; using System.Net.Mime; using System.Security; namespace GSF.Net.Smtp { /// /// A wrapper class to the class that simplifies sending mail messages. /// /// /// This example shows how to send an email message with attachment: /// /// using System; /// using GSF.Net.Smtp; /// /// class Program /// { /// static void Main(string[] args) /// { /// Mail email = new Mail("sender@email.com", "recipient@email.com", "smtp.email.com"); /// email.Subject = "Test Message"; /// email.Body = "This is a test message."; /// email.IsBodyHtml = true; /// email.Attachments = @"c:\attachment.txt"; /// email.Send(); /// email.Dispose(); /// /// Console.ReadLine(); /// } /// } /// /// public class Mail : IDisposable { #region [ Members ] // Constants /// /// Default to be used if one is not specified. /// public const string DefaultSmtpServer = "localhost"; // Fields private string m_from; private string m_toRecipients; private string m_ccRecipients; private string m_bccRecipients; private string m_subject; private string m_body; private string m_attachments; private bool m_isBodyHtml; private string m_username; private SecureString m_password; private bool m_enableSSL; private SmtpClient m_smtpClient; private bool m_disposed; #endregion #region [ Constructors ] /// /// Initializes a new instance of the class. /// public Mail() { } /// /// Initializes a new instance of the class. /// /// The e-mail address of the message sender. public Mail(string from) : this(from, "", DefaultSmtpServer) { } /// /// Initializes a new instance of the class. /// /// The e-mail address of the message sender. /// A comma-separated or semicolon-separated e-mail address list of the message recipients. public Mail(string from, string toRecipients) : this(from, toRecipients, DefaultSmtpServer) { } /// /// Initializes a new instance of the class. /// /// The e-mail address of the message sender. /// A comma-separated or semicolon-separated e-mail address list of the message recipients. /// The name or IP address of the SMTP server to be used for sending the message. public Mail(string from, string toRecipients, string smtpServer) { From = from; ToRecipients = toRecipients; SmtpServer = smtpServer; } /// /// Releases the unmanaged resources before the object is reclaimed by . /// ~Mail() { Dispose(false); } #endregion #region [ Properties ] /// /// Gets or sets the e-mail address of the message sender. /// /// Value being assigned is a null or empty string. public string From { get { return m_from; } set { // This is a required field. if (string.IsNullOrEmpty(value)) throw new ArgumentNullException(nameof(value)); m_from = value; } } /// /// Gets or sets the comma-separated or semicolon-separated e-mail address list of the message recipients. /// /// Value being assigned is a null or empty string. public string ToRecipients { get { return m_toRecipients; } set { m_toRecipients = value; } } /// /// Gets or sets the comma-separated or semicolon-separated e-mail address list of the message carbon copy (CC) recipients. /// public string CcRecipients { get { return m_ccRecipients; } set { m_ccRecipients = value; } } /// /// Gets or sets the comma-separated or semicolon-separated e-mail address list of the message blank carbon copy (BCC) recipients. /// public string BccRecipients { get { return m_bccRecipients; } set { m_bccRecipients = value; } } /// /// Gets or sets the subject of the message. /// public string Subject { get { return m_subject; } set { m_subject = value; } } /// /// Gets or sets the body of the message. /// public string Body { get { return m_body; } set { m_body = value; } } /// /// Gets or sets the name or IP address of the SMTP server to be used for sending the message. /// /// Value being assigned is a null or empty string. public string SmtpServer { get { return m_smtpClient.Host; } set { string[] split; int port; // This is a required field. if (string.IsNullOrEmpty(value)) throw new ArgumentNullException(nameof(value)); #if !MONO // Dispose existing client. if ((object)m_smtpClient != null) m_smtpClient.Dispose(); #endif // Instantiate new client. split = value.Split(':'); if (split.Length == 2 && int.TryParse(split[1], out port)) m_smtpClient = new SmtpClient(split[0], port); else m_smtpClient = new SmtpClient(value); ApplySecuritySettings(); } } /// /// Gets or sets the comma-separated or semicolon-separated list of file names to be attached to the message. /// public string Attachments { get { return m_attachments; } set { m_attachments = value; } } /// /// Gets or sets a boolean value that indicating whether the message is to be formatted as HTML. /// public bool IsBodyHtml { get { return m_isBodyHtml; } set { m_isBodyHtml = value; } } /// /// Gets or sets the username used to authenticate to the SMTP server. /// public string Username { get { return m_username; } set { m_username = value; ApplySecuritySettings(); } } /// /// Gets or sets the password used to authenticate to the SMTP server. /// public string Password { get { return m_password.ToUnsecureString(); } set { m_password = value.ToSecureString(); ApplySecuritySettings(); } } /// /// Gets or sets the password used to authenticate to the SMTP server. /// public SecureString SecurePassword { get { return m_password; } set { m_password = value; ApplySecuritySettings(); } } /// /// Gets or sets the flag that determines whether /// to use SSL when communicating with the SMTP server. /// public bool EnableSSL { get { return m_enableSSL; } set { m_enableSSL = value; if ((object)m_smtpClient != null) m_smtpClient.EnableSsl = value; } } /// /// Gets the object used for sending the message. /// public SmtpClient Client { get { return m_smtpClient; } } #endregion #region [ Methods ] /// /// Releases all the resources used by the object. /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// /// Releases the unmanaged resources used by the object and optionally releases the managed resources. /// /// true to release both managed and unmanaged resources; false to release only unmanaged resources. protected virtual void Dispose(bool disposing) { if (!m_disposed) { try { // This will be done regardless of whether the object is finalized or disposed. if (disposing) { // This will be done only when the object is disposed by calling Dispose(). if ((object)m_smtpClient != null) m_smtpClient.Dispose(); } } finally { m_disposed = true; // Prevent duplicate dispose. } } } /// /// Send the message with to the , /// and using the specified . /// public void Send() { MailMessage emailMessage; emailMessage = new MailMessage(); emailMessage.From = new MailAddress(m_from); emailMessage.Subject = m_subject; emailMessage.Body = m_body; emailMessage.IsBodyHtml = m_isBodyHtml; // Add the specified To recipients for the mail message. if (!string.IsNullOrEmpty(m_toRecipients)) { foreach (string toRecipient in m_toRecipients.Split(';', ',')) emailMessage.To.Add(toRecipient.Trim()); } if (!string.IsNullOrEmpty(m_ccRecipients)) { // Add the specified CC recipients for the mail message. foreach (string ccRecipient in m_ccRecipients.Split(';', ',')) emailMessage.CC.Add(ccRecipient.Trim()); } if (!string.IsNullOrEmpty(m_bccRecipients)) { // Add the specified BCC recipients for the mail message. foreach (string bccRecipient in m_bccRecipients.Split(';', ',')) emailMessage.Bcc.Add(bccRecipient.Trim()); } if (!string.IsNullOrEmpty(m_attachments)) { // Attach the specified files to the mail message. foreach (string attachment in m_attachments.Split(';', ',')) { // Create the file attachment for the mail message. Attachment data = new Attachment(attachment.Trim(), MediaTypeNames.Application.Octet); ContentDisposition header = data.ContentDisposition; // Add time stamp information for the file. header.CreationDate = File.GetCreationTime(attachment); header.ModificationDate = File.GetLastWriteTime(attachment); header.ReadDate = File.GetLastAccessTime(attachment); emailMessage.Attachments.Add(data); // Attach the file. } } try { // Send the mail. m_smtpClient.Send(emailMessage); } finally { // Clean-up. emailMessage.Dispose(); } } private void ApplySecuritySettings() { // If the SMTP client is null, // we cannot apply security settings if ((object)m_smtpClient == null) return; // Set the username and password used to authenticate to the SMTP server if (!string.IsNullOrEmpty(m_username) && (object)m_password != null) m_smtpClient.Credentials = new NetworkCredential(m_username, m_password); else m_smtpClient.Credentials = null; // Apply the flag to enable SSL m_smtpClient.EnableSsl = m_enableSSL; } #endregion #region [ Static ] /// /// Sends a message. /// /// The e-mail address of the message sender. /// A comma-separated or semicolon-separated e-mail address list of the message recipients. /// The subject of the message. /// The body of the message. /// true if the message body is to be formated as HTML; otherwise false. /// The name or IP address of the SMTP server to be used for sending the message. public static void Send(string from, string toRecipients, string subject, string body, bool isBodyHtml, string smtpServer) { Send(from, toRecipients, null, null, subject, body, isBodyHtml, smtpServer); } /// /// Sends a message. /// /// The e-mail address of the message sender. /// A comma-separated or semicolon-separated e-mail address list of the message recipients. /// A comma-separated or semicolon-separated e-mail address list of the message carbon copy (CC) recipients. /// A comma-separated or semicolon-separated e-mail address list of the message blank carbon copy (BCC) recipients. /// The subject of the message. /// The body of the message. /// true if the message body is to be formated as HTML; otherwise false. /// The name or IP address of the SMTP server to be used for sending the message. public static void Send(string from, string toRecipients, string ccRecipients, string bccRecipients, string subject, string body, bool isBodyHtml, string smtpServer) { Send(from, toRecipients, ccRecipients, bccRecipients, subject, body, isBodyHtml, null, smtpServer); } /// /// Sends a message. /// /// The e-mail address of the message sender. /// A comma-separated or semicolon-separated e-mail address list of the message recipients. /// The subject of the message. /// The body of the message. /// true if the message body is to be formated as HTML; otherwise false. /// A comma-separated or semicolon-separated list of file names to be attached to the message. /// The name or IP address of the SMTP server to be used for sending the message. public static void Send(string from, string toRecipients, string subject, string body, bool isBodyHtml, string attachments, string smtpServer) { Send(from, toRecipients, null, null, subject, body, isBodyHtml, attachments, smtpServer); } /// /// Sends a message. /// /// The e-mail address of the message sender. /// A comma-separated or semicolon-separated e-mail address list of the message recipients. /// A comma-separated or semicolon-separated e-mail address list of the message carbon copy (CC) recipients. /// A comma-separated or semicolon-separated e-mail address list of the message blank carbon copy (BCC) recipients. /// The subject of the message. /// The body of the message. /// true if the message body is to be formated as HTML; otherwise false. /// A comma-separated or semicolon-separated list of file names to be attached to the message. /// The name or IP address of the SMTP server to be used for sending the message. public static void Send(string from, string toRecipients, string ccRecipients, string bccRecipients, string subject, string body, bool isBodyHtml, string attachments, string smtpServer) { using (Mail email = new Mail(from, toRecipients, smtpServer)) { email.CcRecipients = ccRecipients; email.BccRecipients = bccRecipients; email.Subject = subject; email.Body = body; email.IsBodyHtml = isBodyHtml; email.Attachments = attachments; email.Send(); } } /// /// Sends a secure message. /// /// The e-mail address of the message sender. /// A comma-separated or semicolon-separated e-mail address list of the message recipients. /// The subject of the message. /// The body of the message. /// true if the message body is to be formated as HTML; otherwise false. /// The name or IP address of the SMTP server to be used for sending the message. /// The username of the account used to authenticate to the SMTP server. /// The password of the account used to authenticate to the SMTP server. public static void Send(string from, string toRecipients, string subject, string body, bool isBodyHtml, string smtpServer, string username, string password) { Send(from, toRecipients, subject, body, isBodyHtml, smtpServer, username, password, true); } /// /// Sends a secure message. /// /// The e-mail address of the message sender. /// A comma-separated or semicolon-separated e-mail address list of the message recipients. /// The subject of the message. /// The body of the message. /// true if the message body is to be formated as HTML; otherwise false. /// The name or IP address of the SMTP server to be used for sending the message. /// The username of the account used to authenticate to the SMTP server. /// The password of the account used to authenticate to the SMTP server. public static void Send(string from, string toRecipients, string subject, string body, bool isBodyHtml, string smtpServer, string username, SecureString password) { Send(from, toRecipients, subject, body, isBodyHtml, smtpServer, username, password, true); } /// /// Sends a secure message. /// /// The e-mail address of the message sender. /// A comma-separated or semicolon-separated e-mail address list of the message recipients. /// The subject of the message. /// The body of the message. /// true if the message body is to be formated as HTML; otherwise false. /// The name or IP address of the SMTP server to be used for sending the message. /// The username of the account used to authenticate to the SMTP server. /// The password of the account used to authenticate to the SMTP server. /// The flag that determines whether to use SSL when communicating with the SMTP server. public static void Send(string from, string toRecipients, string subject, string body, bool isBodyHtml, string smtpServer, string username, string password, bool enableSSL) { Send(from, toRecipients, subject, body, isBodyHtml, smtpServer, username, password.ToSecureString(), enableSSL); } /// /// Sends a secure message. /// /// The e-mail address of the message sender. /// A comma-separated or semicolon-separated e-mail address list of the message recipients. /// The subject of the message. /// The body of the message. /// true if the message body is to be formated as HTML; otherwise false. /// The name or IP address of the SMTP server to be used for sending the message. /// The username of the account used to authenticate to the SMTP server. /// The password of the account used to authenticate to the SMTP server. /// The flag that determines whether to use SSL when communicating with the SMTP server. public static void Send(string from, string toRecipients, string subject, string body, bool isBodyHtml, string smtpServer, string username, SecureString password, bool enableSSL) { using (Mail email = new Mail(from, toRecipients, smtpServer)) { email.Subject = subject; email.Body = body; email.IsBodyHtml = isBodyHtml; email.Username = username; email.SecurePassword = password; email.EnableSSL = enableSSL; email.Send(); } } #endregion } }