using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Net.Mail; namespace WebBlocks { public class ErrorMail : IHttpModule { #region IHttpModule Members public void Init(HttpApplication application) { application.Error += new EventHandler(application_Error); } public void Dispose() { } #endregion public void application_Error(object sender, EventArgs e) { HttpContext ctx = HttpContext.Current; if (!ctx.Request.IsLocal) { //get the inner most exception Exception exception; for (exception = ctx.Server.GetLastError(); exception.InnerException != null; exception = exception.InnerException) { } if (exception is HttpException && ((HttpException)exception).GetHttpCode() == 404) { // don't send an email // } else { // now send it MailMessage msg = new MailMessage(); msg.To.Add(new MailAddress("mjdunn@webadvanced.com")); msg.From = new MailAddress("errors@" + ctx.Request.Url.Host); msg.Subject = "Error of type " + ((HttpException)exception).GetHttpCode().ToString(); msg.IsBodyHtml = false; msg.Body = exception.Message + "\r\n" + exception.StackTrace + "\r\n" + exception.Data; SmtpClient sc = new SmtpClient("webmail.webadvanced.com"); sc.Send(msg); } } } } }