Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / System
Article

How to Send Mail with Error Message Details

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL3 min read 13.2K   2  
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --