title: Send email with Global.asax file
body: <p>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:</p>
  
  <pre><code>&lt;system.web&gt;
      &lt;customErrors mode="On" defaultRedirect="error.aspx" /&gt;
  &lt;/system.web&gt;
  
  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." + "&lt;br&gt;&lt;br&gt;";
              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();
          }
  </code></pre>