在 Outlook VSTO 中快速更改项目的收件人

Quickly changing the recipients of an Item in Outlook VSTO

我正在开发的 Outlook 加载项需要替换 MailItem 的整个 Recipients 集合。官方认可的解决方案包括执行以下操作:

// Clear the old recipients.
while (mailItem.Recipients.Count > 0)
{
    mailItem.Recipients.Remove(1);
}

// Insert the new recipients.
foreach (var newRecipient in newRecipients)
{
    mailItem.Recipients.Add(newRecipient);
}

不幸的是,这根本无法扩展到大量收件人(按数百个收件人的顺序)。对 Recipients 集合的每次更改似乎都会产生大量开销,因为 Outlook 需要刷新 GUI(这表现为撰写 window 中 to/cc/bcc 字段的快速闪烁)。我的实验表明,在我的机器上,与几百个收件人一起完成这个过程需要超过一分钟 (!) 才能完成。

为了缓解这种情况,我提出了以下更快的解决方案:

// Clear the old recipients.
mailItem.To = null;
mailItem.CC = null;
mailItem.BCC = null;

// Insert the new recipients.
// We pretend that the CC and BCC fields are empty for now.
mailItem.To = string.Join(";", newRecipients);

To 属性 是可写的,并且代码似乎运行正确。但是,我担心 VSTO documentation 中的以下评论:

This property contains the display names only. The To property corresponds to the MAPI property PidTagDisplayTo. The Recipients collection should be used to modify this property.

我不确定该如何解释。 To不应该直接操作,为什么是读写?我对 Outlook 专家的问题是:

这是否安全,并且只是没有记录,或者我是否可能通过以这种方式间接操纵收件人 运行 陷入重大问题?

这样就可以了。 MSDN 试图说明的是 reading To 属性 可能只会给你显示名称。当您 设置 那个 属性 时,OOM 将值解析为多个字符串并为所有指定的收件人调用一次 IMessage::ModifyRecipients。当您调用 MailItem.Recipients.Add 时,会为每个收件人调用 IMessage::ModifyRecipients,这不是很有效。