简体   繁体   中英

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)

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.

Firstly, as Eugene noted, you keep retrieving the same default Sent Items folder - use store.GetDefaultFolder instead.

Secondly, you keep resetting the same variable ( items ).

If you want ItemAdd event from Sent Items folder on each store, you need to create a wrapper class that retrieves the Items collection from the store passed as a parameter to the constructor and set up an event handler as a method on the wrapper class. You can then store a list of wrappers in a list to ensure they are alive and can raise events. Something along the lines

List<StoreWrapper> _stores = new List<StoreWrapper>(); //global level
...
foreach (Outlook.Store store in stores)
{
    _stores.Add(new StoreWrapper(store));
}
...
public class StoreWrapper
{
   private Items _items;
   public StoreWrapper(Store store)
   {
      _items = store.GetDefaultFolder(OlDefaultFolders.olFolderSentMail).Items;
      _items.ItemAdd += ItemsAdd;
   }
   private void ItemsAdd(object item)
   {
      //todo
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM