sanjayts
sanjayts

Reputation: 45

How to catch an exception in Outlook Object in C# code if Outlook.MailItem.Send() fails due to invalid recipients

The code below can fail due to a lot of causes, e.g., invalid recipients list or nonexistent recipients. For these problems the sender of the mail will get non-delivered report to his inbox.

What I want to achieve is, if the sent email fails because of improper recipient id, then I should intercept the Exception at Catch branch.

try
{
    Outlook.MailItem mail = OutLookInstance.CreateItem(Outlook.OlItemType.olMailItem);
    mail.Subject = "Send to TAM";
    mail.Recipients.Add("[email protected]");
    mail.Body = "Business Alert mail";
    mail.Display(false);
    mail.OriginatorDeliveryReportRequested = true;
    mail.Send();
}
catch(Exception ex)
{
}

The above code is supposed to do that, but it does not throw any exception if the delivery fails.

How to achieve this using Outlook, please let me know?

Upvotes: 1

Views: 1052

Answers (1)

Eugene Astafiev
Eugene Astafiev

Reputation: 49395

You need to use the Resolve or ResolveAll methods of the Recipient or Recipients class to resolve a Recipient object against the Address Book.

You can find a sample code in C# and VB.NET in the How To: Create and send an Outlook message programmatically article.

Upvotes: 1

Related Questions