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.
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();
}