Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Send Mail with Error Message Details

0.00/5 (No votes)
22 Aug 2012 1  
How to Send Mail with Error Message DetailsSometimes need to handle error and send it to technical support to handle error and send the solution to

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

How to Send Mail with Error Message Details

Sometimes need to handle error and send it to technical support to handle error and send the solution to the clients. anyway we will use Global Application Calss Global.asax to write our code. as you will see

      void Application_Error(object sender, EventArgs e)

        {

            // Code that runs when an unhandled error occurs

            Response.Write("<h2>Error Page</h2>");

           // Get the exception details

            Exception exc = Server.GetLastError();

            Response.Write("Sorry for the inconvenience that may have been caused to you.");

            Response.Write("<b>Error Message</b><p>" + exc.Message + "</p><br>");

           // Clear the error from the server

            Server.ClearError();

            // Report to Administrator

            string url = HttpContext.Current.Request.Url.AbsoluteUri;

            string MailBody = "Website user is getting bad experience on you website. Find the details below:<br><br>";

            MailBody = MailBody + "<br><br><b>Error on Page (Target Webpage): </b>" + url;

            MailBody = MailBody + "<br><br><b>Link on Webpage (Source Webpage): </b>" + Request.UrlReferrer;

            MailBody = MailBody + "<br><br><b>Error Message: </b>" + exc.Message;

            MailBody = MailBody + "<br><br><b>Trace Message: </b>" + exc.StackTrace;

            // Other browser based stuffs

            MailBody = MailBody + "<br><br><b>Platform: </b>" + Request.Browser.Platform;

            MailBody = MailBody + "<br><br><b>Browser: </b>" + Request.Browser.Browser;

            MailBody = MailBody + "<br><br><b>Browser Type: </b>" + Request.Browser.Type;

            MailBody = MailBody + "<br><br><b>Browser Version: </b>" + Request.Browser.Version;

            string MailResponse = sendmail("yourmail@gmail.com", "Bad Experience", MailBody);

            Response.Write("<br><br><b>Note: </b>" + MailResponse.ToString());

        }

       private string sendmail(string ToEmailAdd, string MailSubject, string MailMessageHTMLString)

        {

            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();

            message.To.Add(new System.Net.Mail.MailAddress(ToEmailAdd));

            message.From = new System.Net.Mail.MailAddress("yourmail@gmail.com", "Smart");

            message.Subject = MailSubject;

            message.Body = MailMessageHTMLString;

            message.IsBodyHtml = true;

            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();

            client.Port = 587;

            client.Host = "smtp.gmail.com";

          // client.EnableSsl = true;

            System.Net.NetworkCredential nc = new System.Net.NetworkCredential("yourmail@gmail.com", "your pass");

            //client.UseDefaultCredentials = false;

            client.Credentials = nc;

             try

            {

                client.Send(message);

                return "Automated System success to report this to the Administrator.";

            }

            catch

            {

                return "Automated System not success to report this to the Administrator.";

            }

        }

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here