Outlook GetItemFromID() 总是 Returns 空

Outlook GetItemFromID() Always Returns Null

我目前正在使用 VS 2013 为 MS Outlook 2010 开发一个加载项。我正在使用 NewMailEx 事件处理程序为我提供新电子邮件到达的 EntryID。

获取 EntryIDCollection 没有问题(在进一步移动之前,我确保它是单个 EntryID),但我无法使用给定的 ID 找到实际的电子邮件对象(使用 GetItemFromID()),因此我可以访问新电子邮件的正文。相反,在调用 GetItemFromID() 之后,我只是得到一个空的 MailItem 对象。

public static Outlook.NameSpace olNameSpace;
...
//EVENT: Item Received (new email arrival)
    private static void outLookApp_NewMailEx(string EntryIDCollection)
    {
        try
        {
            //THIS part is failing \/ and returning nothing
            object item = olNameSpace.GetItemFromID(EntryIDCollection, Type.Missing);
            Outlook.MailItem mailItem = item as Outlook.MailItem;

            if (mailItem != null)
            {                    
                //access email object
            }
        }
        catch (Exception e)
        {
            MessageBox.Show("Receive mail exception: " + e);
        }
    }

我是否需要为 GetItemFromID() 指定文件夹,即使它是可选的?

我对访问新电子邮件到达的其他进程持开放态度,但我暂时只是使用 NewMailEx 来获得一个简单的 运行 示例。

谢谢大家

机智,

传递给 GetItemFromID 方法的实际值是多少?

Do I need to specify the folder for GetItemFromID() even though it's optional?

不,不需要指定第二个参数。这是可选的。

我建议阅读以下与在 Outlook 中处理传入电子邮件相关的系列文章:

您确定 return 是 nullGetItemFromID 吗?如果有问题,它将引发异常而不是 return null。当您将 returned 对象转换为 MailItem.

时,很可能是 returns null 的下一行 (Outlook.MailItem mailItem = item as Outlook.MailItem)

确保您没有处理 ReportItemMeetingItem 对象。

感谢大家的帮助。

最终,我无法让 GetItemFromID 方法与 NewMailEx 结合使用。

不过,我确实建议在此处实施 Eugene 的建议:

https://www.add-in-express.com/creating-addins-blog/2011/11/10/outlook-newmail-custom-solution/

这种对文件夹的一致扫描效果很好(在我的实验中具有 100% 的可靠性)。但是,如果您在 Outlook 中使用它时确实遇到任何延迟,我建议增加同步延迟时间,并可能使用 Folder.Items.ItemAddFolders.FolderAdd/FolderChange/FolderRemove 等事件处理程序在同步之间进行检查。

我的加载项需要确保快速处理新电子邮件(用于 links 的安全扫描),因此它不能仅仅在同步之间等待(如果 link 不是在用户到达之前扫描),这就是我推荐那些其他事件处理程序的原因。

再次感谢, ~B-Witty

这里的问题是未初始化的对象olNameSpace。试试这个:

private void outLookApp_NewMailEx(string EntryIDCollection)
    {
        olNameSpace = this.Application.GetNamespace("MAPI");
        try
        {
            //THIS part is failing \/ and returning nothing
            object item = olNameSpace.GetItemFromID(EntryIDCollection, Type.Missing);

            Outlook.MailItem mailItem = item as Outlook.MailItem;



            if (mailItem != null)
            {
                mailItem.Display();
            }
        }
        catch (Exception e)
        {
            MessageBox.Show("" + e);
        }
    }