On the Global.asax page just add the below code
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
// 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 = "youremail@yourdomain.com";
const string FromAddress = "noreply@yourdomain.com";
const string Subject = "An Error Has Occurred on Application Name!";
// Create the MailMessage object
MailMessage msg = new MailMessage();
string[] eAddresses = ToAddress.Split(';');
for (int i = 0; i < eAddresses.Length; i++)
{
if (eAddresses[i].Trim() != "")
{
msg.To.Add(new MailAddress(eAddresses[i]));
}
}
msg.From = (new MailAddress(FromAddress));
msg.Subject = Subject;
msg.IsBodyHtml = true;
msg.Priority = MailPriority.High;
msg.Body = string.Format(@"
<html>
<body>
<h1>An Error Has Occurred!</h1>
<table cellpadding=""5"" cellspacing=""0"" border=""1"">
<tr>
<td text-align: right;font-weight: bold"">URL:</td>
<td>{0}</td>
</tr>
<tr>
<td text-align: right;font-weight: bold"">User:</td>
<td>{1}</td>
</tr>
<tr>
<td text-align: right;font-weight: bold"">Exception Type:</td>
<td>{2}</td>
</tr>
<tr>
<td text-align: right;font-weight: bold"">Message:</td>
<td>{3}</td>
</tr>
<tr>
<td text-align: right;font-weight: bold"">Stack Trace:</td>
<td>{4}</td>
</tr>
</table>
</body>
</html>",
Request.Url,
Request.ServerVariables["LOGON_USER"],
lastErrorTypeName,
lastErrorMessage,
lastErrorStackTrace.Replace(Environment.NewLine, "<br />"));
// Attach the Yellow Screen of Death for this error
string YSODmarkup = lastErrorWrapper.GetHtmlErrorMessage();
if (!string.IsNullOrEmpty(YSODmarkup))
{
Attachment YSOD = Attachment.CreateAttachmentFromString(YSODmarkup, "YSOD.htm");
msg.Attachments.Add(YSOD);
}
// Send the email
SmtpClient smtp = new SmtpClient();
smtp.Send(msg);
Server.Transfer("/Error.aspx");
}