Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to e-mail 404 errors

How to e-mail 404 errors

Scheduled Pinned Locked Moved ASP.NET
csharptutorialquestionasp-netcom
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Larry Antram
    wrote on last edited by
    #1

    I want to be notified of any 404 (Page not found) errors via e-mail. I'm using Windows Server 2008, ASP.NET 3.5, C# code-behind, etc. I have a page, that displays just fine upon 404 error, with the following in code-behind Page_Load():

    // Send Email to Admin
    System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("from@domain.com");
    System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress("to@domain.com");
    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from,to);

    message.Subject = "404 - File or directory not found";
    message.Body = String.Format(
        "The following page generated a 404 error:\\n{0}\\n",
        Convert.ToString(Request.QueryString\["aspxerrorpath"\]) );
    

    System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("mail.domain.com", 2525);

    try
    {
    client.Send(message);
    }
    catch
    {
    }

    I've tried various stuff instead of Request.QueryString["aspxerrorpath"], however I always end up with an e-mail message similar to the following:

    From: from@domain.com [mailto:from@domain.com]
    Sent: Friday, May 14, 2010 4:50 PM
    To: to@domain.com
    Subject: 404 - File or directory not found

    The following page generated a 404 error:

    And that's it. How can I figure out what page generated the error so I can e-mail the URL or location? Specifically, given the above example, what should I replace Request.QueryString["aspxerrorpath"] with? Any ideas appreciated. Thanks in advance.

    L 1 Reply Last reply
    0
    • L Larry Antram

      I want to be notified of any 404 (Page not found) errors via e-mail. I'm using Windows Server 2008, ASP.NET 3.5, C# code-behind, etc. I have a page, that displays just fine upon 404 error, with the following in code-behind Page_Load():

      // Send Email to Admin
      System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("from@domain.com");
      System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress("to@domain.com");
      System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from,to);

      message.Subject = "404 - File or directory not found";
      message.Body = String.Format(
          "The following page generated a 404 error:\\n{0}\\n",
          Convert.ToString(Request.QueryString\["aspxerrorpath"\]) );
      

      System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("mail.domain.com", 2525);

      try
      {
      client.Send(message);
      }
      catch
      {
      }

      I've tried various stuff instead of Request.QueryString["aspxerrorpath"], however I always end up with an e-mail message similar to the following:

      From: from@domain.com [mailto:from@domain.com]
      Sent: Friday, May 14, 2010 4:50 PM
      To: to@domain.com
      Subject: 404 - File or directory not found

      The following page generated a 404 error:

      And that's it. How can I figure out what page generated the error so I can e-mail the URL or location? Specifically, given the above example, what should I replace Request.QueryString["aspxerrorpath"] with? Any ideas appreciated. Thanks in advance.

      L Offline
      L Offline
      Larry Antram
      wrote on last edited by
      #2

      Nevermind, I figured it out. Request.RawUrl or Request.Url works fine.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups