如果有人能告诉我我是否走在正确的轨道上,我将不胜感激......基本上,我有一个网络服务,我需要为我的应用程序运行,我已经把它放入试试,如果尝试失败我希望catch能够向我发送一封包含异常详细信息的电子邮件。
try
{
// run webservice here
}
catch (Exception ex)
{
string strTo = "scott@...";
string strFrom = "web@...";
string strSubject = "Webservice Not Run";
SmtpMail.SmtpServer = "thepostoffice";
SmtpMail.Send(strFrom, strTo, strSubject, ex.ToString());
}
答案 0 :(得分:3)
答案 1 :(得分:1)
只要发生异常,就可以快速简便地发送电子邮件,如下所示:
SmtpClient Server = new SmtpClient();
Server.Host = ""; //example: smtp.gmail.com
Server.Port = ; //example: 587 if you're using gmail
Server.EnableSsl = true; //If the smtp server requires it
//Server Credentials
NetworkCredential credentials = new NetworkCredential();
credentials.UserName = "youremail@gmail.com";
credentials.Password = "your password here";
//assign the credential details to server
Server.Credentials = credentials;
//create sender's address
MailAddress sender = new MailAddress("Sender email address", "sender name");
//create receiver's address
MailAddress receiver = new MailAddress("receiver email address", "receiver name");
MailMessage message = new MailMessage(sender, receiver);
message.Subject = "Your Subject";
message.Body = ex.ToString();
//send the email
Server.Send(message);
答案 2 :(得分:-1)
是的,这是正确的方法,如果您不想使用任何记录器工具。您可以创建函数SendMail(字符串Exceptioin)到您的公共类之一,而不是从每个catch块调用此函数