Ejemplo n.º 1
0
        /// <summary>
        /// Sends a message based on the configured message templates
        /// Allows for body to be formatted with arguments.
        /// </summary>
        /// <param name="messageKey">key of the message configured during the setup</param>
        /// <param name="args">array of arguments (optional)</param>
        private void SendNotificationsPriv(string flowName, string transactionId, string toEmailAddresses, string notificationKey,
                                           string subject, Stream attachmentContent, string attachmentName,
                                           string[] args)
        {
            if (string.IsNullOrEmpty(toEmailAddresses))
            {
                return;
            }
            Activity activity =
                new Activity(NodeMethod.None, flowName, null, ActivityType.Info, transactionId, null, null);

            try
            {
                string messageBody = LoadNotificationBody(notificationKey);
                if (args != null)
                {
                    for (int i = 0; i < args.Length; ++i)
                    {
                        args[i] = HttpUtility.HtmlEncode(args[i]).Replace(Environment.NewLine, "<br/>");
                    }
                    messageBody = string.Format(messageBody, args);
                }
                using (MailMessage msg = new MailMessage(FromEmailAddress,
                                                         toEmailAddresses,
                                                         subject, messageBody))
                {
                    msg.IsBodyHtml = true;
                    if (attachmentContent != null)
                    {
                        Attachment attachment = new Attachment(attachmentContent, attachmentName);
                        msg.Attachments.Add(attachment);
                    }

                    //SaveMailMessage(msg);

                    int retryCount = 3;
                    do
                    {
                        try
                        {
                            SmtpClient smtpClient = new SmtpClient(SmtpHost, SmtpPort);
                            smtpClient.DeliveryMethod = DeliveryMethod;
                            smtpClient.EnableSsl      = _smtpEnableSsl;
                            //If an exception is thrown, this setting causes max cpu usage on a background worker thread for no good reason, so I'm commenting it out:
                            //smtpClient.ServicePoint.MaxIdleTime = 1;
                            if (!string.IsNullOrEmpty(_smtpUsername))
                            {
                                smtpClient.UseDefaultCredentials = false;
                                smtpClient.Credentials           = new System.Net.NetworkCredential(_smtpUsername, _smtpPassword);
                            }
                            smtpClient.Send(msg);
                            retryCount = 1;
                        }
                        catch (Exception smtpException)
                        {
                            if (retryCount > 1)
                            {
                                activity.AppendFormat("Failed to send email notification: From ({0}), To ({1}), Type ({2}), DeliveryMethod ({3}), EnableSsl ({4}), SmtpHost ({5}), SmtpPort ({6}), SmtpUsername ({7})",
                                                      FromEmailAddress, toEmailAddresses, notificationKey,
                                                      DeliveryMethod, _smtpEnableSsl, SmtpHost, SmtpPort, _smtpUsername ?? string.Empty);
                                activity.AppendFormat("EXCEPTION: {0}", ExceptionUtils.ToShortString(smtpException));
                                activity.AppendFormat("Retrying email send ...");
                            }
                            else
                            {
                                throw;
                            }
                        }
                    } while (--retryCount > 0);
                }
                activity.AppendFormat("Successfully sent \"{0}\" email notification to \"{1}\"",
                                      notificationKey, toEmailAddresses);
                ActivityManager.Log(activity);
            }
            catch (Exception ex)
            {
                string message =
                    string.Format("Failed to send email notification: From ({0}), To ({1}), Type ({2}), DeliveryMethod ({3}), EnableSsl ({4}), SmtpHost ({5}), SmtpPort ({6})",
                                  FromEmailAddress, toEmailAddresses, notificationKey,
                                  DeliveryMethod, _smtpEnableSsl, SmtpHost, SmtpPort);
                activity.AppendFormat(message);
                activity.AppendFormat("EXCEPTION: {0}", ExceptionUtils.ToShortString(ex));
                activity.Type = ActivityType.Error;
                ActivityManager.Log(activity);
                LOG.Error(message, ex);
                if (ThrowOnNotificationFailure)
                {
                    throw;
                }
            }
        }