无法从 VSTO Outook 2007 加载项更改邮件 HTML 正文

发布于 2024-10-23 15:42:26 字数 1127 浏览 2 评论 0原文

情节:

我正在 Visual Studio 2010 中使用 VSTO 和 C# 创建 Outlook 2007 加载项。该加载项的目的是跟踪发送给客户的邮件。加载项应将跟踪代码插入到每封出站邮件中,以便在客户回复后能够识别并自动存档。我的目标是在邮件 HTML 的开始标记中插入跟踪代码作为属性:

<html tracking="ddfwer4w5c45cgx345gtg4g" ...>

这将在 Application.ItemSend 事件的事件处理程序中完成:

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\" ...";
    }
}

问题:

它似乎永远不会更改 HTMLBody 属性。并且它不会抛出任何异常。它只是什么都不做。我在Outlook中直接用VBA重写了这个逻辑,并尝试更改HTMLBody,但仍然没有改变。我如何知道它没有更改 HTMLBody 是通过逐步浏览并将鼠标悬停在属性上以查看当前值。

不过,我可以更改 MailItem.Body 属性。但由于我需要以某种方式隐藏跟踪代码,因此 Body 属性对我没有任何帮助。更改 HTMLBody 属性后,我还添加了 MailItem.Save() 方法,但没有更改。

我想也许 ItemSend 事件已经太晚了,我当时无法再更改 HTMLBody,但我找不到任何像 BeforeSend 或类似的事件。

任何想法将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑咖 2024-10-30 15:42:26

我使用 VB 的次数多于 C#,但我想我看到了你的问题。

检查Application_ItemSend 的函数签名。我看到 Cancel 是通过引用传递的; Item 不应该也这样传递吗?

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

如果 Item 是按值传递的,那么您的代码只是操作所创建的副本;原始对象没有被触摸。至少,它在 C++ 中是这样工作的(但我的 C++ 经验是在 .Net 之前的,所以也许这并不适用)。

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).

情绪失控 2024-10-30 15:42:26

我也有类似的需要,需要将一些 ID 放入消息中。看起来 Outlook 在发送电子邮件之前会彻底清理电子邮件,因此在发送电子邮件时,您设置的所有隐藏属性都已被删除。

为了满足我的特殊需求,我最终使用了 MailItemUserProperties 元素。从电子邮件创建到其最终进入“已发送邮件”文件夹,这种情况一直存在。如果客户尝试通过对话回复已发送的电子邮件,也许这种方法对您有用?

mailItem.UserProperties.Add("myid", Outlook.OlUserPropertyType.olText);
mailItem.UserProperties["myid"].Value = myid;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文