0

I am quite new to VSTO and C# programming and am self-learning. I created an Outlook addin that does several things. One of the features is that it takes all sent items and adds them to our DB. When the user sends an email from Outlook it works as expected and prompts/adds it perfectly. But when they sent an email from their phone it only prompt them for the last email sent. I expected that once they open Outlook and it downloads the latest Sent items, it would prompt them for each item to save to the DB. But it is only prompting the last downloaded item, not each one as they download. I think some sort of queue would be reqiured but I can't find any examples. Any help would be greatly appreciated.

here is my code.

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    Outlook.Application application = this.Application;
    _items = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail).Items;
    _items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(SentFolderItemAdded);
}

private void SentFolderItemAdded(object item)
{
// Code to prompt the user and add it to the DB
}
2
  • Do you mean for the messages send before your code was started as Outlook syncs the Sent Items folder on startup? Commented Nov 23, 2022 at 2:39
  • I mean messages sent from a iPhone while Outlook is closed. When outlook open again these message get sync’ed to the sent folder. As they sync I would like my addon to process them. Currently, if 5 messages sync, it only fires for the last one downloaded.
    – Scotty J
    Commented Nov 23, 2022 at 4:43

2 Answers 2

0

Most likely old messages are synchronized to the cached folder before your code has a chance to set up an event sink on the folder on startup.

You can persist (registry etc.) the creation date of the last item you synchronized, and besides watching for the new items using the ItemAdd event, process all items newer than the saved date on startup using Items.Restrict or Items.Find/FindNext with the search criteria based on the saved date.

2
  • That sounds like a good idea. I will put a flag on Items sent from Outlook, so it will only find/fire on Items synced that were sent from external sources. I am new to VSTO/C#, so most of my code is modified from examples. Can you point me to an example of using Items.Restrict or Items.Find/FindNext. No worries if you can't. Thanks for the awesome idea. This will actually help a lot on another project I have in the pipe.
    – Scotty J
    Commented Nov 23, 2022 at 20:36
0

First of all, you need to declare the source object at the global scope (class level) to prevent it from being swiped from the heap by the GC.

Outlook.Items _items = null;

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    Outlook.Application application = this.Application;
    _items = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail).Items;
    _items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(SentFolderItemAdded);
}

private void SentFolderItemAdded(object item)
{
// Code to prompt the user and add it to the DB
}

Be aware, the ItemAdd event is not fired when multiple items are added to the folder (more than sixteen). This is a known issue when dealing with the Outlook object model.

If items are synchronized from the Exchange server before you subscribe to the ItemAdd event you may filter items by using the Find/FindNext or Restrict methods of the Items class. In that case you can deal with items that correspond to the search criteria. Read more about these methods in the following articles that I wrote for the technical blog:

Also you may find a similar solution described for handling incoming emails (for the same situation), see Outlook NewMail unleashed: writing a working solution (C# example).

9
  • Does this line. Outlook.Items _items = null; Declare it at the class level. Is that what you mean? Thank you for the help.
    – Scotty J
    Commented Nov 23, 2022 at 2:02
  • Thanks, I made that change and it did not change the behaviour.
    – Scotty J
    Commented Nov 23, 2022 at 11:47
  • I have a message box where the user must choose whether to save the email. I think this message box may be causing the issue.
    – Scotty J
    Commented Nov 23, 2022 at 11:52
  • Who is moving items to the Sent Items folder? Could you please describe the full scenario in what cases the event is not fired? Commented Nov 23, 2022 at 16:38
  • The way it works is when a new email is sent, immediately after sending, the user is presented with a message box asking if they want to save it to the DB. if they click OK, it runs a process that saves it. The problem is when more than one email is sent using their phone.
    – Scotty J
    Commented Nov 23, 2022 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.