SMTP Send Receive SmtpFailedRecipientException

Here is my problem. I am sending an email to several contacts and I am catching an error if there is an odd email address.

Mostly it works, but if there is more than one invalid email message, I don't get notifications from other bad email addresses.

data = XMLProcessing.LoadAll();

foreach (XMLData.StructReceiver user in data.Receiver)
{
    AddReceiver(user.Mail);
}

SetSubject(data.Body.Subject);
SetMessage(data.Body.Content);

SetSender(data.SenderReply.Sender);
SetReply(data.SenderReply.Replyer);

try
{                
    SMTP.Send(Message);                
}
catch (SmtpFailedRecipientException  e)
{
    if (e.FailedRecipient.ToString() != data.SenderReply.Replyer)
    {
         Failed.Add(e.FailedRecipient.ToString());
    }
}
finally
{
    SMTP.Dispose();
}

      

I get notified by adding a contact to a list and then sending that list to my personal email address, but the catch only happens once, even if there is more than 1 bad address.

+3


source to share


1 answer


See SmtpFailedRecipientsException

. Note that this is a different class, SmtpFailedRecipient s Exception. This class is indeed subclasses SmtpFailedRecipientException

(no s).

You need to catch the SmtpFailedRecipientsException

(more specific type) before you catch the more general one.



In addition to the inherited fields from its parent, it also provides InnerExceptions

(note the plural s , again). This is a collection of send failure exceptions for all addresses . You can repeat this as described in the MSDN article:

try
{
    SMTP.Send(Message);                
}
catch (SmtpFailedRecipientsException exs)
{
    foreach (SmtpFailedRecipientException e in exs)
    {
        if (e.FailedRecipient.ToString() != data.SenderReply.Replyer)
        {
             Failed.Add(e.FailedRecipient.ToString());
        }
    }
}
catch (SmtpFailedRecipientException e)
{
    if (e.FailedRecipient.ToString() != data.SenderReply.Replyer)
    {
         Failed.Add(e.FailedRecipient.ToString());
    }
}
finally
{
    SMTP.Dispose();
}

      

+1


source







All Articles