void Application_Error(object sender, EventArgs e) { // Get the error details HttpException lastErrorWrapper = Server.GetLastError() as HttpException; Exception lastError = lastErrorWrapper; if (lastErrorWrapper.InnerException != null) lastError = lastErrorWrapper.InnerException; string lastErrorTypeName = lastError.GetType().ToString(); string lastErrorMessage = lastError.Message; string lastErrorStackTrace = lastError.StackTrace; const string ToAddress = "support@example.com"; const string FromAddress = "support@example.com"; const string Subject = "An Error Has Occurred!"; // Create the MailMessage object MailMessage mm = new MailMessage(FromAddress, ToAddress); mm.Subject = Subject; mm.IsBodyHtml = true; mm.Priority = MailPriority.High; mm.Body = string.Format(@"

An Error Has Occurred!

URL: User: Exception Type: Message: Stack Trace:
{0}
{1}
{2}
{3}
{4}
", Request.RawUrl, User.Identity.Name, lastErrorTypeName, lastErrorMessage, lastErrorStackTrace.Replace(Environment.NewLine, "
")); // Attach the Yellow Screen of Death for this error string YSODmarkup = lastErrorWrapper.GetHtmlErrorMessage(); if (!string.IsNullOrEmpty(YSODmarkup)) { Attachment YSOD = Attachment.CreateAttachmentFromString(YSODmarkup, "YSOD.htm"); mm.Attachments.Add(YSOD); } // Send the email SmtpClient smtp = new SmtpClient(); smtp.Send(mm); }