Search code examples
c#outlookoffice-interop

How can I access a MailItem after it has been sent?


From my WPF app I create and display a MailItem like following:

using Microsoft.Office.Interop.Outlook;

Application outlook = new Application();
NameSpace ns = outlook.GetNamespace("MAPI");
MailItem mailItem = outlook.CreateItem(OlItemType.olMailItem);
mailItem.Display(false);
string lastEntryId = mailItem.EntryID; // remember EntryId

The user can now compose and send the mail.

After the user did send the mail he confirms the sending in my app. My app then should save the mail in file system. My approach was to "remember" the MailItem.EntryID to use it after sending.

// ... lastEntryId is null therefore this code doesn't work
MailItem mailItem = ns.GetItemFromID(lastEntryId);
string fileName = GetValidFileName(item.Subject) + ".msg";
string file = Path.Combine(GetSaveDirectory(), fileName);
item.SaveAs(file);

But lastEntryId is null before sending therefore I cannot use it.

Question is: how can I access the MailItem after it has been sent?


Solution

  • Even if you had the entry id before the message was sent, it would change when the message is sent and moved to the Sent Items folder. It stays the same only under the PST store.

    Items.ItemAdd event on the Sent Items folder is the earliest you can access the sent message.

    You can also use Application.ItemSend event if you don't care whether the message is in the sent state or not.