1

I'm trying to get my head round the System.Net.Mail class but I'm having trouble finding decent documentation.

First query - Say I have an email with a CC and a BCC email, do I need to catch SmtpFailedRecipientsException instead of SmtpFailedRecipientException (note the singlular Recipient) or is it intended for sending to multiple TO recipients?

Second query - when I iterate through the exceptions using the method below, will the inner exceptions relate to the email addresses that have failed, such as the BCC but not the TO or CC. If this is the case will the resend then go to all 3 again?

    static void CheckExceptionAndResend(SmtpFailedRecipientsException ex, SmtpClient client, MailMessage message)
    {
        var status = ex.StatusCode;

        for (int i = 0; i < ex.InnerExceptions.Length; i++)
        {
            if (status == SmtpStatusCode.MailboxBusy ||
                status == SmtpStatusCode.MailboxUnavailable ||
                status == SmtpStatusCode.TransactionFailed)
            {
                System.Threading.Thread.Sleep(3000);
                client.Send(message);
            }
            else
            {
                // TODO: Log delivery failure
            }
        }
    }

All advice appreciated. Thanks in advance.

4

2 回答 2

1

我不知道也无法回答您的第一个查询,但要回答第二个查询,如果您没有在除此方法之外的任何地方发送消息,我不明白该消息将如何再次或两次发送给收件人..

但是,如果您在由于异常而调用此方法之前使用 smtpclient 实例发送消息,那么是的,消息会再次发送到所有 3 个,即 TO、BCC 和 CC,而不仅仅是发送给失败的收件人。

于 2013-02-28T18:57:22.247 回答
0

对于您的第一个查询,请查看SmtpTransport.SendMail()的源代码,该代码由SmtpClient.Send()调用。如果只有一个收件人(无论是收件人、抄送还是密件抄送)被拒绝,则生成的异常将是 SmtpFailedRecipientException,但如果多个收件人被拒绝,则将是 SmtpFailedRecipientsException。不方便的是,我相信这意味着你需要尝试捕捉这两种类型。

于 2014-09-09T21:23:25.057 回答