SendErrorEmail Method – Sends HTML Email with nicely styled exception stack trace

   

    protected void SendErrorEmail(String subject, String messageBody, Exception ex)
    {


        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("scheduledTasks@yourcompany.com");
        mail.To.Add("your@email.com");       
        mail.Subject = subject;
        mail.IsBodyHtml = true;


        string url = HttpContext.Current.Request.Url.AbsoluteUri;
        string referrer = (HttpContext.Current.Request.UrlReferrer == null) ? "" :
                HttpContext.Current.Request.UrlReferrer.AbsoluteUri;





        string formatting = @"
                            <style type='text/css'>
	                            body { font-family:Verdana; font-size:10pt; }
	                            .msg { font-family:Verdana; font-size:10pt; }
	                            .tbl, .tbl td { font-family:Verdana; font-size:10pt; }

	                            .tbl th { font-weight: bold; text-align: left; padding: 6px 9px 6px 9px; vertical-align: top; }
	                            .tbl td { 
		                            background-color: #f2f2f2; height: 18px; padding: 6px 9px 6px 9px; vertical-align: top;
	                            }
	                            .tbl td.lbl { font-weight: bold; }
	                            .tbl .alt td { background-color: #fff; }
	                            .stackTrace { font-size:8pt; background-color: #ccccc; padding: 10px; }
                            </style>
                            ";


        string preamble = @"
                            <p class='text'>An automated error has been sent at " + DateTime.Now.ToString("M/d/yyyy h:mm:ss tt") + @"</p>
                            <p>Page: <a href='" + url + @"'>" + url + @"</a></p>
                            <p>Referrer: <a href='" + referrer + @"'>" + referrer + @"</a></p>
                            ";

        // Add exception information if it exists
        if (ex != null)
        {

            preamble += @"
                        <p>
	                        <b>Exception Type:</b> " + ex.GetType().FullName + @"<br>
	                        <b>Exception Message:</b> " + ex.Message + @"
                        </p>";
            preamble += @"
                        <pre class='stackTrace'>Stack Trace" + ex.StackTrace + @"</pre>
                        ";

            if (ex.InnerException != null)
            {

                preamble += @"
                            <p>
	                            <b>Inner Exception Type:</b> " + ex.InnerException.GetType().FullName + @"<br>
	                            <b>Inner Exception Message:</b> " + ex.InnerException.Message + @"
                            </p>
                            <pre class='stackTrace'>Stack Trace" + ex.InnerException.StackTrace + @"</pre>
                            ";
            }
        }


        string body = @"
                        <html>
                        <head>
                        " + formatting + @"
                        </head>
                        <body>
                        <div class='msg'>
                        " + preamble + @"                                                                                        
                        <p>" + messageBody + @"</p>
                        </div>
                        </body>
                        </html>
                        ";


        mail.Body = body;

        new SmtpClient().Send(mail);
    }


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s