Wednesday, December 3, 2008

System.Net.Mail throws "no such user" exceptions w/ Lotus Domino!

Code that I had running for years without issues suddenly broke with the error "Mailbox unavailable. The server response was: No such user". Very useful, as with this information, I can now automate (or at least partially automate) internal mailing list management.

For now, I've just added code to catch this specific exception (in addition to the catch-all SmtpException) to have it generate a work order ticket if there's a problem:

try {
} catch(System.Net.Mail.SmtpFailedRecipientException ex) {
//generate work order...

There's also "SmtpFailedRecipientsException" which triggers only if it can't send an email to all the recipients, though I don't see why you would use this over SmtpFailedRecipientException, as the latter informs you in the exception message if all recipients haven't received the email in addition to handling the case where one or some recipients failed.

However, I've noticed that if more than one recipient email fails, it only displays the first email address that failed in the error message. Makes sense, since it probably is processing one email completely at a time, and if it fails on one, it doesn't go any further. The solution to this, I guess, would be to have each email sent out within a loop of separate SMTP calls instead of using the handy "mail.To.Add" method. There would most likely be a performance hit doing it this way, though.

I'm going to keep looking into it, but does anyone know of a better way?

Also, I assume this works with MS Exchange as well, but don't know anyone using email with Exchange to test with. I have verified it doesn't work with Gmail and Hotmail, so this is best used only with an environment where you know that everyone is on the same mail server.

No comments: