Mr. Boy
Mr. Boy

Reputation: 63816

SmtpFailedRecipientException and SmtpFailedRecipientsException

I'm struggling to differentiate these two exceptions especially based on their properties and usage:

Logically, I would expect the plural version to take priority - the server was unable to send your email to any of the recipients - over the server being unable to send to a single specific recipient.

But then what if you send to 8 recipients and two of them fail - now what exception do you get?

What should a properly handled call to SmtpClient.Send() look like in terms of catching SmtpFailedRecipientException, SmtpFailedRecipientsException and SmtpException?

Upvotes: 3

Views: 1797

Answers (1)

Vasileios Gaitanidis
Vasileios Gaitanidis

Reputation: 67

SmtpFailedRecipientsException is a sub-class of SmtpFailedRecipientException. Also SmtpFailedRecipientException is a sub-class of SmtpException.

But then what if you send to 8 recipients and two of them fail - now what exception do you get?

The exception that you will get is an SmtpFailedRecipientsException.

What should a properly handled call to SmtpClient.Send() look like in terms of catching SmtpFailedRecipientException, SmtpFailedRecipientsException and 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
}

Upvotes: 5

Related Questions