Search code examples
c#outlookoffice-interop

Outlook Interop: find mail in sent folder


I create a MailItem from a WPF app and display it to the user:

Application _application = new Application();
MailItem mailItem = _outlook.CreateItem(OlItemType.olMailItem);
mailItem.Display(false);

After user has clicked on Send the MailItem is saved in db (MailItem is then associated with a PK):

_application.ItemSend += Application_ItemSend;
// ...
private void Application_ItemSend(object Item, ref bool Cancel)
{
    MailItem mailItem = Item as MailItem;
    // Save mailItem in db ...
}

After that I need to find the MailItem when it's copied to Outlook's sent folder and mark in in db as 'sent':

private static void TrackSentFolder(Data.Entity.Person person)
{
    MAPIFolder sentFolder = _nameSpace.GetDefaultFolder(OlDefaultFolders.olFolderSentMail);
    // Find previously created MailItem ...

In my research I tried several ways to find MailItem in sent folder:

  1. Mark MailItem using UserProperty before send
  2. Mark MailItem using PropertyAccessor before send
  3. Save MailItem together with PR_SEARCH_KEY in db

I find this rather confusing.

Is there a recommended way to find a previously created MailItem in sent folder?


Solution

  • There is no recommended way of identifying the item after sending it. It is up to you which way is to choose. Every method described in your post can be used.

    Be aware, the Sent Items folder can be skipped if the MailItem.DeleteAfterSubmit property is set. Also the MailItem.SaveSentMessageFolder property allows setting a Folder object that represents the folder in which a copy of the email message will be saved after being sent.