2011-05-19 171 views
2

我有下面这段代码如何在发送邮件时捕获特定的异常?

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()); 
    } 
} 

哪里wl是与颜色写到控制台的快捷方式,并在第一行文字说“的消息无法发送给一个或多个收件人。

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

+0

我结束了关闭本机发送邮件,并使用山魈,而不是开始;) ''wl''只是一个控制台日志包装。 – 2015-08-20 10:29:24

回答

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()); 
    } 

} 
3

你可以尝试这样的财产以后(source):

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

首先要学习如何使用ASP.Net访问this link发送电子邮件。请注意,上述文章中(通过链接铅)的 “SendEmails”仅捕捉一般异常,并在案件ASP.Net 发送电子邮件时它会像“发送电子邮件 失败等”遇到一个错误。我们将扩展上述文章的错误处理功能 。所以让我们开始打开之前创建的解决方案 。我们已经放了一个try-catch块,捕获 一个通用的异常,这个异常告诉我们什么可能已经不存在 错误。让我们赶上不同类型的异常的时候了:

赶上SmtpException:本SmtpException类有一个属性 “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;    
} 

赶上SmtpFailedRecipientException:与相关 电子邮件的收件人如异常的 SmtpFailedRecipientException类交易SMTP无法将电子邮件发送给收件人 。当SmtpClient 无法完成SmtpClient.Send()SmtpClient.SendAsync() 操作向特定对象发生的SmtpFailedRecipientException。为了赶上这个异常使用 下面的代码:

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 
} 

赶上SmtpFailedRecipientsException:本 SmtpFailedRecipientsException实际上是服务于同样的目的 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 
    } 
} 
} 
+0

链接https://web.archive.org/web/20100829145731/http://aspdotnetcode.source-of-humor.com/Articles/Emails/HowToSendEmailFromAspxPageUsingGmailAccount.aspx找不到**由于机器人原因无法抓取或显示页面。文本。** – Kiquenet 2015-08-20 07:05:20