Search code examples
c#outlookvstooutlook-addinadd-in

Outlook VSTO Add-In: Can not resolve recipientname


I'm programming an outlook add-in. I want to modify the mail before it gets sent. Therefore I have registered me for an event before the email gets sent. I can modify it but when I m trying to change the recipient of the mail (so mail.To) it gives me an error (not while my code is running but when outlook tries to sent the mail).

Error says: '...Can not resolve the receivername' (i have translated it so it is not the real error text but close to it)

Here is my code:

void Application_ItemSend(object item, ref bool cancel)
    {
        if (item is Outlook.MailItem mail)
        {
            var to = mail.To;
            var body = mail.Body;
            var editedBody = to + "#" + body;
            mail.Body = editedBody;
            mail.To = @"<another email>";
        }
    }


private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        //Register the new event
        Globals.ThisAddIn.Application.ItemSend += Application_ItemSend;
    }

Solution

  • You are resetting all To recipients. Is that what you really want to do? Try to use MailItem.Recipients.Add (retuns Recipient object) followed by Recipient.Resolve.

    You are also setting the plain text Body property wiping out all formatting. Consider using HTMLBody instead, just keep in mind that two HTML strings must be merged rather than concatenated to produce valid HTML.