Reputation: 663
I have the following code in my application:
catch (SmtpException oSMTPException)
{
string sFailedEmailIds = string.Empty;
string[] sMailids = null;
((System.Net.Mail.SmtpFailedRecipientsException)(oSMTPException)).
InnerExceptions[0].FailedRecipient
}
I have a number of mail IDs to which the mail has not been sent.
I want to store the details in a string sFailedEmailIds
.
How can I do that?
Upvotes: 2
Views: 1702
Reputation: 21
I don't exactly know what you want to store, but I guess you want all the email IDs, so this is something similar I recently did:
catch (SmtpFailedRecipientsException e)
{
string sFailedEmailIds = "";
for (int i = 0; i < e.InnerExceptions.Length; i++)
{
SmtpStatusCode status = e.InnerExceptions[i].StatusCode;
if (status == SmtpStatusCode.MailboxBusy ||
status == SmtpStatusCode.MailboxUnavailable) {
sFailedEmailIds += e.InnerExceptions[i].FailedRecipient.Split('<', '>')[1] + ", ";
} else {
// other statuscode that you want to handle
}
}
return sFailedEmailIds;
}
I use the SmtpFailedRecipientsException instead of SmtpException because it has all the emails that failed.
Hope it helps. ;)
Upvotes: 2
Reputation: 6401
string sFailedEmailIds = String.Join(",", ((System.Net.Mail.SmtpFailedRecipientsException)(oSMTPException)).InnerExceptions.Select(x=>x.FailedRecipient));
Upvotes: 0