Search code examples
c#outlookvstooutlook-addinoffice-addins

Call a function to get the Sent Date when an e-mail is sent and that works with all types of connected e-mail - Outlook - VSTO


I did two different implementations to solve my problem, which is: I need that when an email is sent, it calls a function that with it I can get The Sent Date. Both implementations work, not 100% but they work.

    private void ThisAddIn_Startup(object sender, System.EventArgs e) {

        //First implementation.
        try
        {
            Outlook.Stores stores = Application.Session.Stores;
            foreach (Outlook.Store store in stores)
            {
                var folder = store.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
                items = folder.Items;
                items.ItemAdd += ItemsAdd;
            }

        }
        catch (Exception exception)
        {
            Log.Error(exception.Message);
            throw;
        }

        //Second implementation.
        this.Application.ItemSend += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
    }

First implementation: With it I can return the correct Sent Date, however it is not working with all types of emails that are being used in the Outlook App, for example, I have my microsoft email that works with it, and a business email that was connected as POP3 and with this account does not work.

Second implementation: It works with all types of e-mails but gets the wrong Sent Date.

Is there a solution in the first implementation to work with accounts connected as POP3? or another implementation that solves my problem?

Translated with www.DeepL.com/Translator (free version)


Solution

  • You are subscribing to the same folder when you iterate over all stores in the profile:

                foreach (Outlook.Store store in stores)
                {
                    var folder = store.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
                    items = folder.Items;
                    items.ItemAdd += ItemsAdd;
                }
    

    Instead, you need to use the store object to subscribe to a store-specific Sent Items folder.

    foreach (Outlook.Store store in stores)
                {
                    var folder = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
                    items = folder.Items;
                    items.ItemAdd += ItemsAdd;
                }
    

    Also it makes sense to keep a list of Items objects because each time the code overwrites the object reference, so previous objects could be released by the GC at some point of time.

    The Store.GetDefaultFolder method returns 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.

    The Application.ItemSend event is fired before the item is sent out, moreover the process can be cancelled by others. So, you can't be sure the item is sent at that stage.