需要捕获 SMTP 发送电子邮件代码的正确错误
Need to catch the proper error for SMTP send email code
出于某种原因,我的程序不使用 SMTP 服务器发送电子邮件,它在这段代码的某处停止,我无法调试它,因为它发生在编译的 .exe
public static void sendLog(MailMessage mail) {
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress(Constants.EMAIL_SENDER);
mail.To.Add(Constants.EMAIL_RECEIVER);
mail.Subject = Environment.UserName;
SmtpServer.Port = 587;
SmtpServer.Credentials = new NetworkCredential(Constants.EMAIL_SENDER, Constants.EMAIL_SENDER_PASSWORD);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
我需要知道 try/catch 使用什么,并处理正确的异常以写入 .txt 文件?
您应该使用正确的SMTP 例外
catch (SmtpException ex)
{
// write to log file
string msg = "Failure sending email"+ex.Message+" "+ex.StackTrace;
}
出于某种原因,我的程序不使用 SMTP 服务器发送电子邮件,它在这段代码的某处停止,我无法调试它,因为它发生在编译的 .exe
public static void sendLog(MailMessage mail) {
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress(Constants.EMAIL_SENDER);
mail.To.Add(Constants.EMAIL_RECEIVER);
mail.Subject = Environment.UserName;
SmtpServer.Port = 587;
SmtpServer.Credentials = new NetworkCredential(Constants.EMAIL_SENDER, Constants.EMAIL_SENDER_PASSWORD);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
我需要知道 try/catch 使用什么,并处理正确的异常以写入 .txt 文件?
您应该使用正确的SMTP 例外
catch (SmtpException ex)
{
// write to log file
string msg = "Failure sending email"+ex.Message+" "+ex.StackTrace;
}