1

The plot:

I am creating an outlook 2007 add-in using VSTO and C# in Visual Studio 2010. The purpose of the add-in is to cause tracking of mails sent to customers. The add-in should insert a tracking code into every outbound mail that later enables it to be recognized and auto archived once the customer replies. My goal is to insert a tracking code as an attribute in the opening tag of the mail's HTML:

<html tracking="ddfwer4w5c45cgx345gtg4g" ...>

This will be done in the event handler of the Application.ItemSend event:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    this.Application.ItemSend += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
}

void Application_ItemSend(object Item, ref bool Cancel)
{
    if (Item is Outlook.MailItem)
    {
        Outlook.MailItem mail = (Outlook.MailItem)Item;
        mail.HTMLBody = "<html tracking=\"4w5te45yv5e6ye57j57jr6\" ...";
    }
}

The problem:

It seem to never change the HTMLBody property. And it does not throw any exceptions. It just does nothing. I rewrote this logic directly in VBA in Outlook, and tried to change the HTMLBody, but it still did not change. How I know it did not change the HTMLBody is by stepping through and hovering over the property to see the current value.

I am however able to make changes to the MailItem.Body property. But since I need to hide the tracking code in some way, the Body property does not help me at all. I also added the MailItem.Save() method after changing the HTMLBody property, but no change.

I thought that perhaps the ItemSend event is to late and I can't change the HTMLBody at that time anymore, but I can't find any event like BeforeSend or alike.

Any ideas would be much appreciated.

Justus Burger
  • 271
  • 2
  • 4

2 Answers2

0

I had a similar need to put some ids into the message. Seems like Outlook thoroughly scrubs the email before sending it, so by the time it is sent, all the nice hidden properties that you set have been removed.

For my particular need, I ended up instead using the UserProperties element of MailItem. This persists from when the email is created to when it ends up in the Sent Items folder. Perhaps this approach can work for you if the customer reply is tried to the sent email through a conversation?

mailItem.UserProperties.Add("myid", Outlook.OlUserPropertyType.olText);
mailItem.UserProperties["myid"].Value = myid;
fredw
  • 1,409
  • 12
  • 23
0

I use VB a lot more than C#, but I think I see your problem.

Check the function signature of Application_ItemSend. I see that Cancel is passed by reference; shouldn't Item be passed that way too?

void Application_ItemSend(ref object Item, ref bool Cancel)
{
    // ...
}

If Item is passed by value, then your code is only manipulating the copy that was made; the original object isn't being touched. At least, that's how it worked in C++ (but my C++ experience is before .Net, so maybe that doesn't apply).

Allan W
  • 580
  • 1
  • 3
  • 8