4

Here is my problem. I am sending an email to few contacts and I am catching the error if there is a unvalid email addresse.

Basicly, it is working, but if there is more than 1 invalid email, I am not receiving notification from others bad email addresse.

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 am receiving notification by adding the contact into a list and then sending this list to my personnal email addresse, but the catch only happen one time, even if there is more than 1 bad addresse.

Wanceslas
  • 107
  • 1
  • 1
  • 10
  • this is what a catch block is supposed to do... once an error is caught it never goes back to the code to check whether more errors are still there...?? – Akshay Soam Apr 21 '15 at 20:44
  • I don't get why he still process other good email then? Even if I build my contact list with a bad addresse at first position, it will passe every good addresse after this one. – Wanceslas Apr 21 '15 at 20:46

1 Answers1

2

See SmtpFailedRecipientsException. Notice that this is a different class, SmtpFailedRecipientsException. This class actually sub-classes SmtpFailedRecipientException (no s).

You will want to catch SmtpFailedRecipientsException (the more specific type) before catching the more general one.

In addition to inherited fields from its parent, it also provides InnerExceptions (notice the plural s, again). This is a collection of exceptions about send failures for all addresses. You can iterate through that as described by 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();
}
EyasSH
  • 3,679
  • 22
  • 36
  • Thanks! I've not notice that there was another class for multiple exceptions... Do I still need to capture with SmtpFailedRecipientException if there is only 1 exception? – Wanceslas Apr 21 '15 at 20:51
  • 1
    This part is unclear and the MSDN documentations do not make it clear. The question you are asking is equivalent to "Are instances of `SmtpFailedRecipientException` also instances of `SmtpFailedRecipientsException`?". The documentation is unclear here. You can try it out in testing and figure out the answer, though it is probably *ok* to leave a more general `catch` block after the specific one that might not be triggered. – EyasSH Apr 21 '15 at 20:55
  • 1
    I've done some test and conclude that elements catched from SmtpFailedRecipientsException are not instances of SmtpFailedRecipientException. So if there is only one error throw by the send function, SmtpFailedRecipientsException won't be call. – Wanceslas Apr 22 '15 at 15:03