Пример #1
0
        /// <summary>
        /// Send exception details as HTML e-mail.
        /// </summary>
        /// <param name="ex">Exception to generate email from.</param>
        /// <param name="subject">Use custom subject instead of generated from exception</param>
        void _SendMail(Exception ex, string subject, string body)
        {
            var smtp    = SmtpClientEx.Current;
            var message = new MailMessage();

            MailHelper.ApplyRecipients(message, smtp.SmtpFrom, smtp.ErrorRecipients);
            message.Subject    = subject;
            message.Body       = Current.ExceptionInfo(ex, body);
            message.IsBodyHtml = true;
            SendMail(message);
            message.Dispose();
        }
Пример #2
0
        /// <summary>
        /// Send exception details as HTML e-mail.
        /// </summary>
        /// <param name="ex">Exception to generate email from.</param>
        /// <param name="subject">Use custom subject instead of generated from exception</param>
        public void SendMail(Exception ex, string subject = null, string body = null)
        {
            var smtp    = SmtpClientEx.Current;
            var message = new MailMessage();

            MailHelper.ApplyRecipients(message, smtp.SmtpFrom, smtp.ErrorRecipients);
            //------------------------------------------------------
            // Subject
            //------------------------------------------------------
            // If exception found then...
            if (ex != null)
            {
                if (ex.Data != null)
                {
                    var key = ex.Data.Keys.Cast <object>().FirstOrDefault(x => object.ReferenceEquals(x, "StackTrace"));
                    if (key != null && ex.Data[key] is StackTrace)
                    {
                        ex.Data.Remove(key);
                    }
                }
                // If subject was not specified
                if (string.IsNullOrEmpty(subject))
                {
                    subject = GetSubjectPrefix(ex, TraceEventType.Error) + ex.Message;
                }
            }
            if (string.IsNullOrEmpty(subject))
            {
                subject = "null";
            }
            subject      = RxBreaks.Replace(subject, " ");
            subject      = RxMultiSpace.Replace(subject, " ");
            message.Body = body;
            try
            {
                // Cut subject because some mail servers refuse to deliver messages when subject is too large.
                var maxLength = 255;
                message.Subject = (subject.Length > maxLength)
                                        ? subject.Substring(0, maxLength - 3) + "..."
                                        : subject;
            }
            catch (Exception)
            {
                message.Subject = "Bad subject";
                message.Body   += "<div>Subject:" + subject + "</div>\r\n";
            }
            message.IsBodyHtml = true;
            //------------------------------------------------------
            SendMail(message);
            message.Dispose();
        }
Пример #3
0
        /// <summary>
        /// Send exception details as HTML e-mail.
        /// </summary>
        /// <param name="ex">Exception to generate email from.</param>
        /// <param name="subject">Use custom subject instead of generated from exception</param>
        void _SendMail(Exception ex, string subject, string body)
        {
            var smtp = SmtpClientEx.Current;
            var m    = new MailMessage();

            MailHelper.ApplyRecipients(m, smtp.SmtpFrom, smtp.ErrorRecipients);
            // Add headers, which can be used on server side to group errors.
            m.Headers.Add(XLogHelperErrorSource, ex.Source);
            m.Headers.Add(XLogHelperErrorType, ex.GetType().FullName);
            m.Headers.Add(XLogHelperErrorCode, ex.HResult.ToString());
            m.Subject    = subject;
            m.Body       = Current.ExceptionInfo(ex, body);
            m.IsBodyHtml = true;
            SendMail(m);
            m.Dispose();
        }
Пример #4
0
        /// <summary>
        /// Mail will be sent to error recipient if not LIVE.
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="cc"></param>
        /// <param name="bcc"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="isBodyHtml"></param>
        /// <param name="preview">Force preview on LIVE system.</param>
        /// <param name="rethrow">Throw exception if sending fails. Must be set to false when sending exceptions.</param>
        /// <param name="attachments"></param>
        public Exception SendMailFrom(string @from, string @to, string cc, string bcc, string subject, string body, bool isBodyHtml, bool rethrow = false, string[] attachments = null, SmtpDeliveryMethod?overrideDeliveryMethod = null, bool forcePreview = false)
        {
            Exception error = null;
            // Re-throw - throw the error again to catch by a caller
            var message = new MailMessage();
            var smtp    = new SmtpClientEx();

            try
            {
                MailHelper.ApplyRecipients(message, @from, @to, cc, bcc);
                MailHelper.ApplyAttachments(message, attachments);
                message.IsBodyHtml = isBodyHtml;
                message.Subject    = subject;
                message.Body       = body;
                // Override delivery method.
                if (overrideDeliveryMethod.HasValue)
                {
                    smtp.DeliveryMethod = overrideDeliveryMethod.Value;
                }
                SendMail(message, smtp, forcePreview);
            }
            catch (Exception ex)
            {
                if (!ex.Data.Contains("Mail.DeliveryMethod"))
                {
                    ex.Data.Add("Mail.DeliveryMethod", overrideDeliveryMethod);
                }
                if (!ex.Data.Contains("Mail.From"))
                {
                    ex.Data.Add("Mail.From", @from);
                }
                if (!string.IsNullOrEmpty(@to) && !ex.Data.Contains("Mail.To"))
                {
                    ex.Data.Add("Mail.To", @to);
                }
                if (!string.IsNullOrEmpty(cc) && !ex.Data.Contains("Mail.Cc"))
                {
                    ex.Data.Add("Mail.Cc", cc);
                }
                if (!string.IsNullOrEmpty(bcc) && !ex.Data.Contains("Mail.Bcc"))
                {
                    ex.Data.Add("Mail.Bcc", bcc);
                }
                if (!string.IsNullOrEmpty(subject) && !ex.Data.Contains("Mail.Subject"))
                {
                    ex.Data.Add("Mail.Subject", subject);
                }
                // Will be processed by the caller.
                if (rethrow)
                {
                    throw;
                }
                else
                {
                    Current.ProcessException(ex);
                }
                error = ex;
            }
            finally
            {
                // Attachments and message will be disposed.
                message.Dispose();
                smtp.Dispose();
            }
            return(error);
        }