Folder vs. GetDefaultFolder issue

时间:2015-07-28 22:15:50

标签: c# outlook-addin

I am having an issue with an outlook add-in that I am writing. Everything was working perfectly until I made one change and I cannot figure out why this is.

Here is my original code:

inbox = outlookNameSpace.GetDefaultFolder(
            Microsoft.Office.Interop.Outlook.
            OlDefaultFolders.olFolderInbox);

Here is the change I made:

inbox = outlookNameSpace.Folders["sharedinbox"].Folders["inbox"];

I did this to access a specific inbox folder that is shared across multiple users because all the users have their default folders then they have this sharedinbox as well. When an email comes in I have an event that logs the email which works perfectly. I have a worker in the background that iterates through all the items in that folder and checks if that specific email is read then does some stuff. For some reason when I mark the email as read now it doesn't recognize it. When I did it with my original code it worked perfectly. Is there a difference between GetDefaultFolder and Folder that would cause this kind of issue?

1 个答案:

答案 0 :(得分:0)

  

我在后台有一个工作人员遍历该文件夹中的所有项目,并检查是否已读取该特定电子邮件,然后执行某些操作。

Office应用程序(Outlook)使用单线程单元模型,该模型不允许在辅助线程上使用Outlook对象模型。相反,您可以考虑使用低级代码 - 扩展MAPI。或者只是该API的任何包装器(例如,最着名的一个 - Redemption)。

  

出于某种原因,当我将电子邮件标记为已读时,它无法识别它。

您是否在主线程上运行代码?

  

GetDefaultFolder和Folder之间是否存在导致此类问题的区别?

在这两种情况下,您都应该获得有效的Folder对象。但是要获取共享文件夹,我建议使用Namespace类的GetSharedDefaultFolder方法。