SmtpFailedRecipientException and SmtpFailedRecipientsExcepti
本文关键字:SmtpFailedRecipientsExcepti and SmtpFailedRecipientException | 更新日期: 2023-09-27 18:17:46
我很难区分这两个异常,特别是基于它们的属性和用法:
- 它们都为单个电子邮件地址定义了一个
FailedRecipient
属性 - 如果您捕获
SmtpFailedRecipientException
,则屏蔽SmtpFailedRecipientsException
从逻辑上讲,我希望复数形式优先——服务器无法将您的电子邮件发送给任何收件人——而不是服务器无法发送给单个特定收件人。
但是,如果你发送给8个收件人,其中两个失败了,那么你会得到什么异常?
在捕获SmtpFailedRecipientException
, SmtpFailedRecipientsException
和SmtpException
方面,正确处理的SmtpClient.Send()
调用应该是什么样子的?
SmtpFailedRecipientsException是SmtpFailedRecipientException的子类。SmtpFailedRecipientException
也是SmtpException的子类。
但是,如果你发送给8个收件人,其中两个失败了,那么你会得到什么异常?
您将得到的异常是SmtpFailedRecipientsException
。
正确处理的SmtpClient.Send()调用应该是什么样子的捕获SmtpFailedRecipientException的条款,SmtpFailedRecipientsException和SmtpException?
try {
smtpClient.Send(mailMessage);
}
catch (SmtpFailedRecipientsException recipientsException)
{
Console.WriteLine($"Failed recipients: {string.Join(", ", recipientsException.InnerExceptions.Select(fr => fr.FailedRecipient))}");
// your code here
}
catch (SmtpFailedRecipientException recipientException)
{
Console.WriteLine($"Failed recipient: {recipientException.FailedRecipient}");
// your code here
}
catch (SmtpException smtpException)
{
Console.WriteLine(smtpException.Message);
// your code here
}