COMException in Outlook Online (non-cached) Mode only

0

The example code shown below illustrates the issue I'm having, namely when I start Outlook in Online mode I cannot get most of the properties of the mail item passed to the OutboxItems_ItemAdd handler. The error returned is:

Attachments = {System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x8004010F
   --- End of inner exception stack trace ---
   at System.Runtime...

I do NOT get this error when attempting to retrieve the properties of the mail item in the SentItems_ItemAdd handler. Also, it is important to note that everything works perfectly when in Outlook cached mode; the issue in the Outbox handler is only when Outlook starts in Online mode. Is this a bug, or am I just doing something wrong?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;

namespace OnlineErrorTest{
public partial class ThisAddIn{

    Outlook.Folder sentFolder;
    Outlook.Folder outboxFolder;
    Outlook.Items sentItems;
    Outlook.Items outboxItems;

    private void ThisAddIn_Startup(object sender, System.EventArgs e) {
        sentFolder = this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail) as Outlook.Folder;
        outboxFolder = this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox) as Outlook.Folder;
        sentItems = sentFolder.Items;
        outboxItems = outboxFolder.Items;
        sentItems.ItemAdd += SentItems_ItemAdd;
        outboxItems.ItemAdd += OutboxItems_ItemAdd;
    }

    private void OutboxItems_ItemAdd(object Item) {
        Outlook.MailItem mi = Item as Outlook.MailItem;
        Outlook.Recipients r = mi.Recipients; //CAUSES EXCEPTION //System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x8004010F
    }

    private void SentItems_ItemAdd(object Item) {
        Outlook.MailItem mi = Item as Outlook.MailItem;
        Outlook.Recipients r = mi.Recipients; //WORKS FINE
    }

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


    private void InternalStartup(){
        this.Startup += new System.EventHandler(ThisAddIn_Startup);
        this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
    }
}

}

c#
outlook
vsto
outlook-addin
asked on Stack Overflow Feb 27, 2020 by public wireless

1 Answer

1

The error code is MAPI_E_NOT_FOUND, which means the item no longer exists - this is hardly surprising: by the time your code gets to it, Exchange Server most likely already sent the message and moved it to the Sent Items folder.

You should never touch the messages being submitted - even if you succeed in doing so, touching an item with OOM aborts the submission process. Use Application.ItemSend event instead - it is your last chance to access the item before it is handed over to the spooler and sent.


User contributions licensed under CC BY-SA 3.0