发送邮件时如何捕获特定的异常?

Chr*_*ård 3 .net c# exception-handling

我有以下代码

try
{
  if (!bDebug)
    smtp.Send(m);
}
catch (Exception e)
{
  wl("Meldingen kunne ikke sendes til en eller flere mottakere.", ConsoleColor.Red);
  wl(e.Message, ConsoleColor.DarkRed);
  using (var errorfile = System.IO.File.CreateText("error-" + DateTime.Now.Ticks + ".txt"))
  {
    errorfile.WriteLine(e.StackTrace);
    if (e.GetType() == typeof(SmtpFailedRecipientException))
    {
      var se = (SmtpFailedRecipientException) e;
      errorfile.WriteLine(se.FailedRecipient);
    }
    errorfile.WriteLine(e.ToString());
  }
}
Run Code Online (Sandbox Code Playgroud)

wl使用颜色向控制台写入的快捷方式在哪里,第一行中的文本显示"无法将消息发送给一个或多个收件人.

以前我只抓住了SmtpFailedRecipientException它,但是当它开始在其他一些步骤中失败时,我把通用推到Exception那里.所以我想知道的部分是我将Exception对象转换为更具体的对象来获取FailedRecipient属性.可以/应该以另一种更恰当的方式完成吗?看起来有点笨重......

JK.*_*JK. 8

您可以拥有多个catch分支:

catch (SmtpFailedRecipientException se)
{
  using (var errorfile = System.IO.File.CreateText("error-" + DateTime.Now.Ticks + ".txt"))
  {
    errorfile.WriteLine(se.StackTrace);  
    // variable se is already the right type, so no need to cast it      
    errorfile.WriteLine(se.FailedRecipient);       
    errorfile.WriteLine(se.ToString());
  }
}
catch (Exception e)
{
  wl("Meldingen kunne ikke sendes til en eller flere mottakere.", ConsoleColor.Red);
  wl(e.Message, ConsoleColor.DarkRed);   

  // for other error types just write the info without the FailedRecipient
  using (var errorfile = System.IO.File.CreateText("error-" + DateTime.Now.Ticks + ".txt"))
  {
    errorfile.WriteLine(e.StackTrace);        
    errorfile.WriteLine(e.ToString());
  }

}
Run Code Online (Sandbox Code Playgroud)


dan*_*iax 5

你可以尝试这样的事情(来源):

我们将学习如何捕获/处理使用 ASP.Net 发送电子邮件时可能发生的不同类型的异常/错误。我们将使用System.Net.Mail.

首先要了解如何使用 ASP.Net 发送电子邮件,请访问此链接。请注意,在上面的文章(由链接引导)中,“SendEmails”仅捕获一般异常,如果 ASP.Net 在发送电子邮件时遇到错误,它会类似于“发送电子邮件失败等”。我们将扩展上述文章的错误处理功能。因此,让我们先打开我们之前创建的解决方案。我们已经放置了一个 try-catch 块来捕获一个通用异常,该异常很少说明可能出错的地方。让我们立即捕获不同类型的异常:

抓住SmtpExceptionSmtpException该类有一个属性“ StatusCode”,它实际上是一个枚举,用于在传输电子邮件时获取 SMTP 服务器返回的错误/异常代码值。它还提供了在电子邮件发送过程中可能发生的错误/异常的更多详细信息。例如

catch (SmtpException smtpException) 
{ // You can put a switch block to check for different exceptions or errors    

 // To checks if the destination mailbox is busy 
 if (smtpException.StatusCode == SmtpStatusCode.MailboxBusy) 
   throw smtpException; 

 // To check if the client is authenticated or is allowed to send email using the specified SMTP host 
 if (smtpException.StatusCode == SmtpStatusCode.ClientNotPermitted) 
   throw smtpException; 
 // The following code checks if the email message is too large to be stored in destination mailbox 

 if (smtpException.StatusCode == SmtpStatusCode.ExceededStorageAllocation) 
   throw smtpException; 
 // To check if the email was successfully sent to the SMTP service 

 if (smtpException.StatusCode == SmtpStatusCode.Ok) 
   throw smtpException; 
 // When the SMTP host is not found check for the following value 

 if (smtpException.StatusCode == SmtpStatusCode.GeneralFailure) 
   throw smtpException;             
} 
Run Code Online (Sandbox Code Playgroud)

Catch the SmtpFailedRecipientExceptionSmtpFailedRecipientException该类处理与电子邮件收件人相关的异常,例如 SMTP 无法将电子邮件发送给收件人。SmtpFailedRecipientExceptionSmtpClient 无法完成 对特定收件人的操作SmtpClient.Send()SmtpClient.SendAsync()操作时会发生这种情况。要捕获此异常,请使用以下代码:

catch (System.Net.Mail.SmtpFailedRecipientException smtpFailedRecipientException) 
{ 
 // Get the email that is causing email sending failed exception 
 String emailCausingException = smtpFailedRecipientException.FailedRecipient; 

 // Get the status code why and what is actually causing an email sending error 
 System.Net.Mail.SmtpStatusCode statusCode = smtpFailedRecipientException.StatusCode; 

 // Take some action either re-send the email again or do some error handling code here 
}
Run Code Online (Sandbox Code Playgroud)

Catch the SmtpFailedRecipientsExceptionSmtpFailedRecipientsException实际上是一组SmtpFailedRecipientException用于相同目的的 对象。当SmtpClient无法向一个或多个收件人发送电子邮件时,它用于处理异常。

catch (System.Net.Mail.SmtpFailedRecipientsException smtpFailedRecipientsException) 
{ 
 ArrayList emailCausingException = new ArrayList(); 

 foreach (SmtpFailedRecipientException smtpFailedRecipientException 
               in smtpFailedRecipientsException.InnerExceptions) 
{ 
   // Get the email that is causing email sending failed exception 
   // Add it to a list of emails with exceptions 
   emailCausingException.Add(smtpFailedRecipientException.FailedRecipient);

   // Get the status code why and what is actually causing an email sending error 
   System.Net.Mail.SmtpStatusCode statusCode = smtpFailedRecipientException.StatusCode; 
   // Take some action either re-send the email again or do some error handling 
   // You can also log or print this status code for an individual recipient here 
   if (statusCode == SmtpStatusCode.MailboxBusy) 
   { 
     //Re-Send email after some time 
     System.Threading.Thread.Sleep(1000); 
     //smtpClient.Send(); 
     //Email sending code here 
   } 
 } 
}
Run Code Online (Sandbox Code Playgroud)