Hello All,

I want to send the email if there is any Exception in my asp.net site.

Please help me.

Thank in Advance.

If you don't already have a Global.asax then right click on the project and select "Add -- New Item" then select "Global Application Class". Modify your Global.asax:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Net.Mail;
using System.Net;

namespace daniweb.asp
{
  public class Global : System.Web.HttpApplication
  {

    protected void Application_Start(object sender, EventArgs e)
    {

    }

    protected void Session_Start(object sender, EventArgs e)
    {

    }

    protected void Application_BeginRequest(object sender, EventArgs e)
    {

    }

    protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {

    }

    protected void Application_Error(object sender, EventArgs e)
    {
      Exception lastError = Server.GetLastError();
      lastError = (lastError == null || lastError.InnerException == null ? lastError : lastError.InnerException);
      if (lastError != null)
      {
        int? httpErrorCode = default(int?);
        {
          HttpException httpEx = (lastError as HttpException);
          if (httpEx != null)
          {
            httpErrorCode = httpEx.GetHttpCode();
          }
        }


        MailMessage m = new MailMessage();
        m.From = new MailAddress("sender@host.com", "sender");
        m.To.Add("recipient@host.com");
        m.Subject = "Web Exception" + (httpErrorCode == null ? string.Empty : " HTTP ERROR: " + ((int)httpErrorCode).ToString("F0"));
        m.Body = lastError.Message + Environment.NewLine + Environment.NewLine +
          lastError.StackTrace;
        SmtpClient smtp = new SmtpClient("email.server.com");
        smtp.Credentials = new NetworkCredential("username", "password");
        smtp.Send(m);
      }
    }

    protected void Session_End(object sender, EventArgs e)
    {

    }

    protected void Application_End(object sender, EventArgs e)
    {

    }
  }
}

If you don't already have a Global.asax then right click on the project and select "Add -- New Item" then select "Global Application Class". Modify your Global.asax:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Net.Mail;
using System.Net;

namespace daniweb.asp
{
  public class Global : System.Web.HttpApplication
  {

    protected void Application_Start(object sender, EventArgs e)
    {

    }

    protected void Session_Start(object sender, EventArgs e)
    {

    }

    protected void Application_BeginRequest(object sender, EventArgs e)
    {

    }

    protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {

    }

    protected void Application_Error(object sender, EventArgs e)
    {
      Exception lastError = Server.GetLastError();
      lastError = (lastError == null || lastError.InnerException == null ? lastError : lastError.InnerException);
      if (lastError != null)
      {
        int? httpErrorCode = default(int?);
        {
          HttpException httpEx = (lastError as HttpException);
          if (httpEx != null)
          {
            httpErrorCode = httpEx.GetHttpCode();
          }
        }


        MailMessage m = new MailMessage();
        m.From = new MailAddress("sender@host.com", "sender");
        m.To.Add("recipient@host.com");
        m.Subject = "Web Exception" + (httpErrorCode == null ? string.Empty : " HTTP ERROR: " + ((int)httpErrorCode).ToString("F0"));
        m.Body = lastError.Message + Environment.NewLine + Environment.NewLine +
          lastError.StackTrace;
        SmtpClient smtp = new SmtpClient("email.server.com");
        smtp.Credentials = new NetworkCredential("username", "password");
        smtp.Send(m);
      }
    }

    protected void Session_End(object sender, EventArgs e)
    {

    }

    protected void Application_End(object sender, EventArgs e)
    {

    }
  }
}

Problem Solved
Thank you very much Scott

regards,

Gajanan

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.