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