65.9K
CodeProject is changing. Read more.
Home

How to Send Mail with Error Message Details

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Oct 11, 2013

CPOL

3 min read

viewsIcon

13681

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

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.";

            }

        }