In the event of an unhandled error I would like to send an email to the admin that contains the information from the error that occurred. Below is what I have in my web.config and Global.asax.cs file, the redirect works but the email does not:
<system.web>
<customErrors mode="On" defaultRedirect="error.aspx" />
</system.web>
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
// Get the exception object.
Exception error = Server.GetLastError();
MailMessage mail = new MailMessage();
mail.To.Add("admin@mysite.com");
mail.Subject = "Error";
mail.Body = "Somebody has experienced an error." + "<br><br>";
mail.Body += error.ToString();
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("username", "password");
smtp.Port = 587;
smtp.Send(mail);
Server.ClearError();
}