none
GetDefaultFolder(OlDefaultFolders.olFolderSentMail) returns the wrong folder RRS feed

  • Question

  • We have an Outlook add-in, so that when someone sends an email and that email meets certain rules, the email is uploaded to a DMS.

    In order to do this, we get the Sent Items folder, then attach to the ItemAdd event of the sent items folder's items collection.

    The code looks like this:

                    _outlookApp = this.Application;
                    using (ClosableComItem<Outlook.NameSpace> nameSpace = new ClosableComItem<Outlook.NameSpace>(_outlookApp.GetNamespace("MAPI")))
                    {
                        _sentFolder = nameSpace.Item.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderSentMail);
                        _sentItems = _sentFolder.Items;
                        _sentItems.ItemAdd += new Microsoft.Office.Interop.Outlook.ItemsEvents_ItemAddEventHandler(SentItems_ItemAdd);
                        _outlookApp.ItemSend += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
                    }

    We have been using this in Outlook 2007 for several years now. We recently upgraded the project to an Outlook 2013 add-in and have upgraded 3 users to 2013. One of them has a strange issue - the user has an additional mailbox set up (as an additional mailbox), and for some reason the event is binding to the sent items folder in the second mailbox rather than the personal one. I have tested this by sending an email, observing that the event fails to fire, then dragging and dropping that email into the sent items folder in the second mailbox, whereupon the event does fire.

    Obviously the default sent items folder should be the one in the primary mailbox, so any ideas why this function call is returning the wrong one?


    Wednesday, August 19, 2015 2:10 PM

Answers

  • Hello Steve,

    Do not use multiple dots in the single line of code! Each time you call a property or method you get a new object instance.

     _sentFolder = nameSpace.Item.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders...);

    Where did you find the Item property of the Namespace class?

    Try to use instead:

    _sentFolder = nameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders...

    The GetDefaultFolder method of the Namespace class returns a Folder object that represents the default folder of the requested type for the current profile. You may find the GetDefaultFolder method of the Store class which allows to get  a Folder object that represents the default folder in the store and that is of the type specified by the FolderType argument. This method is similar to the GetDefaultFolder method of the NameSpace object. The difference is that this method gets the default folder on the delivery store that is associated with the account, whereas NameSpace.GetDefaultFolder returns the default folder on the default store for the current profile.

    • Proposed as answer by André Santo Wednesday, August 19, 2015 2:20 PM
    • Marked as answer by Steve O Jones Thursday, August 20, 2015 10:16 AM
    Wednesday, August 19, 2015 2:17 PM