Search code examples
c#outlook-addin

Folder vs. GetDefaultFolder issue


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?


Solution

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

    Office applications (Outlook) use the single threaded apartment model which doesn't allow to use the Outlook object model on secondary threads. Instead, you may consider using a low-level code - Extended MAPI. Or just any wrapper around that API (for example, the most famous one - Redemption).

    For some reason when I mark the email as read now it doesn't recognize it.

    Do you run the code on the main thread?

    Is there a difference between GetDefaultFolder and Folder that would cause this kind of issue?

    In both cases you should get a valid Folder object. But to get a shared folder I'd recommend using the GetSharedDefaultFolder method of the Namespace class instead.