发送电子邮件后,消息 ID (PR_INTERNET_MESSAGE_ID_W_TAG) 在事件处理程序中不可用

Message Id (PR_INTERNET_MESSAGE_ID_W_TAG) Not Available in Event Handler After Sending An Email

我有一个 Microsoft.Office.Interop.Outlook.Application 的实例,我将 itemSendHandler 添加到 Application.itemSend

Application.itemSend += itemSendHandler

我假设在 itemSendHandler 中,电子邮件应该已经发送,并且应该存在电子邮件的消息 ID。然而下面的代码产生了一个 null messageId:

private void itemSendEventHandler(object sentItem, ref bool Cancel)
}
    string PR_INTERNET_MESSAGE_ID_W_TAG = "http://schemas.microsoft.com/mapi/proptag/0x1035001F";
    PropertyAccessor propertyAccessor = ((MailItem)sentItem).PropertyAccessor;
    // This is null? Why?
    string messageId = (string)propertyAccessor.GetProperty(PR_INTERNET_MESSAGE_ID_W_TAG);
    ThisAddIn.attemptToReleaseComObject(propertyAccessor);   
{

但是当我随后立即通过代码或工具(如 OutlookSpy 或 MFCMAPI)检查发送的项目时,属性 存在。为什么在发送事件处理程序期间 messageId 为空?

您需要先保存消息,然后才能访问 属性。即使那样,PR_INTERNET_MESSAGE_ID 也可能无法在缓存模式下访问 - 出于性能原因,Outlook 不会将“已发送邮件”文件夹中的项目与其在线副本同步,并且只有邮件的在线版本才会具有 属性。您可以使用扩展 MAPI(C++ 或 Delphi)或使用 MAPI_NO_CACHE 标志的 Redemption(任何语言)以在线模式打开该消息,但在 OOM 中无法做到这一点。