A wrapper class to the MailMessage class that simplifies sending mail messages.
Inheritance: IDisposable
Example #1
0
 /// <summary>
 /// Creates a new instance of the <see cref="EmailNotifier"/>.
 /// </summary>
 public EmailNotifier()
 {
     m_variableNames = new HashSet<string>();
     m_keyMapping = new Dictionary<MeasurementKey, string>();
     m_nonAliasedTokens = new SortedDictionary<int, string>();
     m_expressionContext = new ExpressionContext();
     m_mailClient = new Mail();
 }
Example #2
0
File: Mail.cs Project: rmc00/gsf
 /// <summary>
 /// Sends a secure <see cref="Mail"/> message.
 /// </summary>
 /// <param name="from">The e-mail address of the <see cref="Mail"/> message sender.</param>
 /// <param name="toRecipients">A comma-separated or semicolon-separated e-mail address list of the <see cref="Mail"/> message recipients.</param>
 /// <param name="subject">The subject of the <see cref="Mail"/> message.</param>
 /// <param name="body">The body of the <see cref="Mail"/> message.</param>
 /// <param name="isBodyHtml">true if the <see cref="Mail"/> message body is to be formated as HTML; otherwise false.</param>
 /// <param name="smtpServer">The name or IP address of the SMTP server to be used for sending the <see cref="Mail"/> message.</param>
 /// <param name="username">The username of the account used to authenticate to the SMTP server.</param>
 /// <param name="password">The password of the account used to authenticate to the SMTP server.</param>
 /// <param name="enableSSL">The flag that determines whether to use SSL when communicating with the SMTP server.</param>
 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();
     }
 }
Example #3
0
File: Mail.cs Project: rmc00/gsf
 /// <summary>
 /// Sends a <see cref="Mail"/> message.
 /// </summary>
 /// <param name="from">The e-mail address of the <see cref="Mail"/> message sender.</param>
 /// <param name="toRecipients">A comma-separated or semicolon-separated e-mail address list of the <see cref="Mail"/> message recipients.</param>
 /// <param name="ccRecipients">A comma-separated or semicolon-separated e-mail address list of the <see cref="Mail"/> message carbon copy (CC) recipients.</param>
 /// <param name="bccRecipients">A comma-separated or semicolon-separated e-mail address list of the <see cref="Mail"/> message blank carbon copy (BCC) recipients.</param>
 /// <param name="subject">The subject of the <see cref="Mail"/> message.</param>
 /// <param name="body">The body of the <see cref="Mail"/> message.</param>
 /// <param name="isBodyHtml">true if the <see cref="Mail"/> message body is to be formated as HTML; otherwise false.</param>
 /// <param name="attachments">A comma-separated or semicolon-separated list of file names to be attached to the <see cref="Mail"/> message.</param>
 /// <param name="smtpServer">The name or IP address of the SMTP server to be used for sending the <see cref="Mail"/> message.</param>
 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();
     }
 }
        // Send an email address notifying the admin of a changes in the flat-lined status of measurements.
        private void SendEmailNotification(IEnumerable<IMeasurement> measurements, bool flatlined)
        {
            Ticks now = DateTime.Now.Ticks;
            Mail message = new Mail("*****@*****.**", m_adminEmailAddress, m_smtpServer);
            StringBuilder body = new StringBuilder();

            body.AppendLine("Measurement Key, Value, Timestamp");

            foreach (IMeasurement measurement in measurements)
            {
                body.Append(measurement.Key);
                body.Append(", ");
                body.Append(measurement.AdjustedValue);
                body.Append(", ");
                body.Append(measurement.Timestamp);
                body.AppendLine();

                if (flatlined)
                    m_lastNotified[measurement.Key] = now;
                else
                    m_lastNotified.Remove(measurement.Key);
            }

            if (flatlined)
                message.Subject = "Flat-lined measurements";
            else
                message.Subject = "No longer flat-lined measurements";

            message.Body = body.ToString();
            message.Send();
            message.Dispose();
        }
Example #5
0
        private static void EmailReport(Arguments args, string subject, string reportFilePath)
        {
            const string CryptoKey = "0679d9ae-aca5-4702-a3f5-604415096987";
            string smtpServer, fromAddress, toAddresses;
            string username, password;

            if (TryGetValue(args, "smtpServer", out smtpServer) &&
                TryGetValue(args, "fromAddress", out fromAddress) &&
                TryGetValue(args, "toAddresses", out toAddresses) &&
                smtpServer.Length > 0 &&
                fromAddress.Length > 0 &&
                toAddresses.Length > 0)
            {
                using (Mail message = new Mail(fromAddress, toAddresses, smtpServer))
                {
                    message.Subject = subject;
                    message.Attachments = reportFilePath;
                    message.IsBodyHtml = true;
                    message.Body = string.Format(
                        "<div>\r\n" +
                        "The attached report requires a portable document format (PDF) reader, such as the " +
                        "<a href=\"http://get.adobe.com/reader/\">Adobe Acrobat PDF Reader</a>.\r\n" +
                        "</div>\r\n" +
                        "<br><br>\r\n" +
                        "<div>\r\n" +
                        "<i>E-mail generated at {0:yyyy-MM-dd HH:mm:ss.fff} UTC.</i>\r\n" +
                        "</div>",
                        DateTime.UtcNow);

                    if (TryGetValue(args, "username", out username) &&
                        TryGetValue(args, "password", out password) &&
                        username.Length > 0 &&
                        password.Length > 0)
                    {
                        message.EnableSSL = true;
                        message.Username = username;
                        message.Password = password.Decrypt(CryptoKey, CipherStrength.Aes256);
                    }

                    message.Send();
                }
            }
        }
Example #6
0
        /// <summary>
        /// Logs encountered <see cref="Exception"/> to an e-mail message.
        /// </summary>
        /// <param name="exception"><see cref="Exception"/> that was encountered.</param>
        protected virtual void ExceptionToEmail(Exception exception)
        {
            // Log if enabled.
            if (m_logToEmail && !string.IsNullOrEmpty(m_contactEmail) && !m_suppressInteractiveLogging)
            {
                m_logToEmailOK = false;

                Mail email = new Mail(m_contactEmail, m_contactEmail);

                email.Subject = string.Format("Exception in {0} at {1}", ApplicationName, DateTime.Now.ToString());
                email.Body = GetExceptionInfo(exception, m_logUserInfo);
                email.Attachments = GetEmailAttachments();
                email.SmtpServer = m_smtpServer;
                email.Send();

                m_logToEmailOK = true;
            }
        }
Example #7
0
        private static void EmailReport(Arguments args, string subject, string reportFilePath)
        {
            string smtpServer, fromAddress, toAddresses;

            if (TryGetValue(args, "smtpServer", out smtpServer) &&
                TryGetValue(args, "fromAddress", out fromAddress) &&
                TryGetValue(args, "toAddresses", out toAddresses) &&
                smtpServer.Length > 0 &&
                fromAddress.Length > 0 &&
                toAddresses.Length > 0)
            {
                using (Mail message = new Mail(fromAddress, toAddresses, smtpServer))
                {
                    message.Subject = subject;
                    message.Attachments = reportFilePath;
                    message.IsBodyHtml = true;
                    message.Body = string.Format(
                        "<div>\r\n" +
                        "The attached report requires a portable document format (PDF) reader, such as the " +
                        "<a href=\"http://get.adobe.com/reader/\">Adobe Acrobat PDF Reader</a>.\r\n" +
                        "</div>\r\n" +
                        "<br><br>\r\n" +
                        "<div>\r\n" +
                        "<i>E-mail generated at {0:yyyy-MM-dd HH:mm:ss.fff} UTC.</i>\r\n" +
                        "</div>",
                        DateTime.UtcNow);

                    message.Send();
                }
            }
        }
Example #8
0
        private void SendEmail(string subject, string message, string details)
        {
            if (string.IsNullOrEmpty(m_emailRecipients))
                throw new ArgumentNullException("EmailRecipients");

            Mail briefMessage = new Mail(m_emailSender, m_emailSender, m_emailServer);
            Mail detailedMessage = new Mail(m_emailSender, m_emailSender, m_emailServer);

            briefMessage.Subject = subject;
            detailedMessage.Subject = subject;
            detailedMessage.Body = message + "\r\n\r\n" + details;
            foreach (string recipient in m_emailRecipients.Replace(" ", "").Split(';', ','))
            {
                string[] addressParts = recipient.Split(':');
                if (addressParts.Length > 1)
                {
                    if (string.Compare(addressParts[1], "sms", true) == 0)
                    {
                        // A brief message is to be sent.
                        briefMessage.ToRecipients = addressParts[0];
                        briefMessage.Send();
                    }
                }
                else
                {
                    // A detailed message is to be sent.
                    detailedMessage.ToRecipients = recipient;
                    detailedMessage.Send();
                }
            }
        }