title: User Error Alert
body: <p>I am using VS2010 (with C#). I have an online application. I have 2 error forms setup that the user is redirected to if they encounter an error. I have a global.asax.cs file that i have code setup in the <code>Application_Error</code> event to send me an email to let me know a user has received an error. My issue is that I am receving an email every time the form loads. It should be only when an error occurs. Here is my code behind in the <code>global.asax.cs</code> file:</p>
  
  <pre><code>protected void Application_Error(object sender, EventArgs e)
  {
      if (HttpContext.Current.Server.GetLastError() != null)
      {
          Exception myException = HttpContext.Current.Server.GetLastError().GetBaseException();
  
          // Create the Outlook application by using inline initialization.
          Outlook.Application oApp = new Outlook.Application();
  
          Outlook.MailItem email = (Outlook.MailItem)(oApp.CreateItem(Outlook.OlItemType.olMailItem));
  
          email.Recipients.Add(fake@fakeemail.com);
  
          email.Subject ="Error in COF page " + Request.Url.ToString();
  
          string message = string.Empty;
  
          message +="&lt;b&gt;Message&lt;/b&gt;&lt;br /&gt;" + myException.Message + "&lt;br /&gt;";
  
          message +="&lt;b&gt;StackTrace&lt;/b&gt;&lt;br /&gt;" + myException.StackTrace + "&lt;br /&gt;";
  
          message +="&lt;b&gt;Query String&lt;/b&gt;&lt;br /&gt;" + Request.QueryString.ToString() + "&lt;br /&gt;";
  
          email.Body = message;
  
          email.Send();
  
           //Clear out any lingering errors after they have been reported. 
          Server.ClearError();
      }
  }
  </code></pre>