Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Application_Error - problem sending email from global.asax

Posted on 2009-06-28
6
Medium Priority
?
693 Views
Last Modified: 2012-05-07
I have a global.asax file and I'm trying to use the Application_Error method and display properties from GetLastError() and then email those errors to myself.  When I test my application for errors, the email never gets sent to me.  And, it doesn't appear the global.asax events are firing.  I tried setting breakpoint in the global.asax file, but the breakpoint is never hit.

I know my smtp settings in my web.config file work because I can test my contact form and the email sends to me fine.  So, I'm not sure what I'm missing in the global.asax file and why the error emails are not getting sent to me.

See below for the code in my global.asax file.

Thanks for any help.
<%@ Application Language="C#" %>
<%@ Import Namespace="System.Net.Mail" %>
<%@ Import Namespace="System.Configuration" %>
 
<script runat="server">
 
    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup
 
    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown
 
    }
        
    void Application_Error(object sender, EventArgs e) 
    {
        HttpContext ctx = HttpContext.Current;
 
        Exception exception = ctx.Server.GetLastError();
 
        string message = "<table width=\"85%\" border=\"0\" align=\"center\" cellpadding=\"5\" cellspacing=\"1\" style='background-color: #000099;'>";
        message += "<tr bgcolor=\"#eeeeee\">";
        message += "<td colspan=\"2\" style='font-family: Verdana;font-size: 12px;color: #000099;font-weight: bold;'>Page Error</td>";
        message += "</tr>";
        message += "<tr>";
        message += "<td width=\"100\" align=\"right\" bgcolor=\"#eeeeee\" style='font-family: Verdana;font-size: 11px;color: #000099;font-weight: bold;' nowrap>Error</td>";
        message += "<td bgcolor=\"#FFFFFF\" style='font-family: Verdana;font-size: 11px;'>" + exception.Message.ToString() + "</td>";
        message += "</tr>";
        message += "<tr>";
        message += "<td width=\"100\" align=\"right\" bgcolor=\"#eeeeee\" style='font-family: Verdana;font-size: 11px;color: #000099;font-weight: bold;' nowrap>Details</td>";
        message += "<td bgcolor=\"#FFFFFF\" style='font-family: Verdana;font-size: 11px;'>" + exception.InnerException.Message.ToString() + "</td>";
        message += "</tr>";
        message += "<tr>";
        message += "<td width=\"100\" align=\"right\" bgcolor=\"#eeeeee\" style='font-family: Verdana;font-size: 11px;color: #000099;font-weight: bold;' nowrap>URL</td>";
        message += "<td bgcolor=\"#FFFFFF\" style='font-family: Verdana;font-size: 11px;'>" + Request.Url.AbsoluteUri.ToString() + "</td>";
        message += "</tr>";
        message += "<tr>";
        message += "<td width=\"100\" align=\"right\" bgcolor=\"#eeeeee\" style='font-family: Verdana;font-size: 11px;color: #000099;font-weight: bold;' nowrap>Stack Trace</td>";
        message += "<td bgcolor=\"#FFFFFF\" style='font-family: Verdana;font-size: 11px;'>" + exception.InnerException.StackTrace.ToString() + "</td>";
        message += "</tr>";
        message += "<tr>";
        message += "<td width=\"100\" align=\"right\" bgcolor=\"#eeeeee\" style='font-family: Verdana;font-size: 11px;color: #000099;font-weight: bold;' nowrap>Request Host</td>";
        message += "<td bgcolor=\"#FFFFFF\" style='font-family: Verdana;font-size: 11px;'>" + Request.UserHostAddress.ToString() + "</td>";
        message += "</tr>";
        message += "<tr>";
        message += "<td width=\"100\" align=\"right\" bgcolor=\"#eeeeee\" style='font-family: Verdana;font-size: 11px;color: #000099;font-weight: bold;' nowrap>Host Name</td>";
        message += "<td bgcolor=\"#FFFFFF\" style='font-family: Verdana;font-size: 11px;'>" + Request.UserHostName.ToString() + "</td>";
        message += "</tr>";
        message += "<tr>";
        message += "<td width=\"100\" align=\"right\" bgcolor=\"#eeeeee\" style='font-family: Verdana;font-size: 11px;color: #000099;font-weight: bold;' nowrap>User Agent</td>";
        message += "<td bgcolor=\"#FFFFFF\" style='font-family: Verdana;font-size: 11px;'>" + Request.UserAgent.ToString() + "</td>";
        message += "</tr>";
        message += "<tr>";
        message += "<td width=\"100\" align=\"right\" bgcolor=\"#eeeeee\" style='font-family: Verdana;font-size: 11px;color: #000099;font-weight: bold;' nowrap>URL Referrer</td>";
        message += "<td bgcolor=\"#FFFFFF\" style='font-family: Verdana;font-size: 11px;'>" + Request.UrlReferrer.ToString() + "</td>";
        message += "</tr>";
        message += "<tr>";
        message += "<td width=\"100\" align=\"right\" bgcolor=\"#eeeeee\" style='font-family: Verdana;font-size: 11px;color: #000099;font-weight: bold;' nowrap>UserName</td>";
        message += "<td bgcolor=\"#FFFFFF\" style='font-family: Verdana;font-size: 11px;'>" + Request.LogonUserIdentity.Name.ToString() + "</td>";
        message += "</tr>";
        message += "<tr>";
        message += "<td width=\"100\" align=\"right\" bgcolor=\"#eeeeee\" style='font-family: Verdana;font-size: 11px;color: #000099;font-weight: bold;' nowrap>Method</td>";
        message += "<td bgcolor=\"#FFFFFF\" style='font-family: Verdana;font-size: 11px;'>" + exception.TargetSite.ToString() + "</td>";
        message += "</tr>";
        message += "<tr>";
        message += "<td width=\"100\" align=\"right\" bgcolor=\"#eeeeee\" style='font-family: Verdana;font-size: 11px;color: #000099;font-weight: bold;' nowrap>Source</td>";
        message += "<td bgcolor=\"#FFFFFF\" style='font-family: Verdana;font-size: 11px;'>" + exception.Source.ToString() + "</td>";
        message += "</tr>";
        message += "</table>";
        
        // Compose Email
        MailMessage msg = new MailMessage();
 
        msg.From = new MailAddress(ConfigurationManager.AppSettings["ApplicationErrorFrom"].ToString());
        msg.To.Add(ConfigurationManager.AppSettings["ApplicationErrorTo"].ToString());    
        msg.Subject = "Error occurred in personal website application";
        msg.IsBodyHtml = true;
        msg.Body = message;
 
        SmtpClient smtp = new SmtpClient();
        smtp.Send(msg);
 
        ctx.Server.ClearError();
 
    }
 
    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started
 
    }
 
    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.
 
    }
       
</script>

Open in new window

0
Comment
Question by:-Dman100-
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 3

Expert Comment

by:eirikurh
ID: 24731722
Add error handling to your send method and print out the error message.

        try
        {
            SmtpClient smtp = new SmtpClient();
            smtp.Send(msg);
        }
        catch (Exception ex)
        {
            #if (DEBUG)            
                Response.Write(ex.Message);
                Response.End();
            #endif
        }

Let me know what error message you get.
If the smtp host is missing "The SMTP host was not specified get"

Add the smtp host you want to use to send to your web.config
<system.net>
<mailSettings>
<smtp>
<network host="xxx.xxx.xx.xxx" port="25" />
</smtp>
</mailSettings>
</system.net>


0
 

Author Comment

by:-Dman100-
ID: 24731773
Hi eirikurh,

I added a try/catch block around my send method, but no error message thrown.  I'm not even able to debug the global.asax file.  It doesn't appear that the global.asax events are firing.

The smtp host is definately specified in my web.config file:

<system.net>
    <mailSettings>
      <smtp>
        <network host="xxx.xxx.xx.xxx" port="25"/>
      </smtp>
    </mailSettings>
  </system.net>

I've tried deleting and re-adding the global.asax file, but still the events do not appear to be getting fired.

I have no idea what is going on?

Thanks for your help.
0
 
LVL 3

Expert Comment

by:eirikurh
ID: 24731791
When you set your breakpoint to Applocation_Error method

Can you send me the code where the error occurs

If you catch your error  using try,catch, then Application_Error never gets executed.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:-Dman100-
ID: 24731851
I was finally able to debug into the global.asax file.  I'm getting a null reference on inner exception (see attached)?


screenshot.png
0
 
LVL 3

Accepted Solution

by:
eirikurh earned 2000 total points
ID: 24732075
Inner Exception is probable null, you need to add check for the Message and StackTrace

        if (exception.InnerException != null)
        {
            message += "<tr>";
            message += "<td width=\"100\" align=\"right\" bgcolor=\"#eeeeee\" style='font-family: Verdana;font-size: 11px;color: #000099;font-weight: bold;' nowrap>Details</td>";
            message += "<td bgcolor=\"#FFFFFF\" style='font-family: Verdana;font-size: 11px;'>" + exception.InnerException.Message.ToString() + "</td>";
            message += "</tr>";
        }

        if (exception.InnerException != null)
        {
        message += "<tr>";
        message += "<td width=\"100\" align=\"right\" bgcolor=\"#eeeeee\" style='font-family: Verdana;font-size: 11px;color: #000099;font-weight: bold;' nowrap>Stack Trace</td>";
        message += "<td bgcolor=\"#FFFFFF\" style='font-family: Verdana;font-size: 11px;'>" + exception.InnerException.StackTrace.ToString() + "</td>";
        message += "</tr>";
        }

I would also add check if the Request.UrlReferrer is null

        if (Request.UrlReferrer != null)
        {
            message += "<tr>";
            message += "<td width=\"100\" align=\"right\" bgcolor=\"#eeeeee\" style='font-family: Verdana;font-size: 11px;color: #000099;font-weight: bold;' nowrap>URL Referrer</td>";
            message += "<td bgcolor=\"#FFFFFF\" style='font-family: Verdana;font-size: 11px;'>" + Request.UrlReferrer.ToString() + "</td>";
            message += "</tr>";
        }

0
 

Author Closing Comment

by:-Dman100-
ID: 31597691
Thank you for the help!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question