none
Intermittent error when saving mail item in OnItemSend event for mail item. System.Runtime.InteropServices.COMException (0x8004010A): The operation cannot be performed because the object has been deleted. RRS feed

  • Question

  • We get intermittent error when saving mail item in OnItemSend event for mail item. System.Runtime.InteropServices.COMException (0x8004010A): The operation cannot be performed because the object has been deleted.

    Here are the code snippets:

    // class Connect
    _application.ItemSend += OnItemSend; // Subscribe to OnItemSend

    // class SendThruItemEvents


    private void OnItemSend(object olItem, ref bool cancel) // Send button is clicked on new email
    {
    ...
    var locMailItem = olItem as MailItem;
    if (locMailItem != null)
    {
            editorSession.UpdateMailItem(locMailItem); // see method below
            editorSession.Send();                      // see method below
    }
    ...
    }

    =====================

    // class EditorSession
    public void UpdateMailItem(MailItem olNewItem)
    {
        ((MailItemEx)mailItem).Update(olNewItem); // see method below
    }

    public void Send()
    {
    if (!sendCalled)
            {
    sendCalled = true;
                    try
                    {
                        mailItem.IsSending = true;
                        mailItem.Save();   // calls Send, see below              
                        FireSending();
                    }
                    catch (ActionIsAbortedException ex)
                    {
                        ImapSaveWorkaround.ProcessException(ex);                    
                        OneShotTimer.Schedule(() => sendCalled = false);
                        mailItem.IsSending = false;
                        throw;
                    }
    }
    }

    =============================

    // class MailItemEx
    private _MailItem olMailItem;

    public void Update(_MailItem item)
            {
                var olNewItem = Com.CloneRef(item);
                olMailItem = olNewItem;
                olMailItem.BeforeAutoSave += OlMailItem_BeforeAutoSave;
                olMailItem.Write += OlMailItem_Write;
                UpdateItemId();            
            }

     protected virtual void UpdateItemId()
            {
                try
                {
                    object mapiobject = OlMailItem.MAPIOBJECT;
                    using (Com.Object(mapiobject))
                    {
                        itemId = MAPIHelper.GetItemId(mapiobject);
                    }
                }
                catch (Exception ex)
                {
                    throw AttachedEmailWorkaround.IsAttachedMsg(ex);
                }
            }

     public void Save()
                {
                    
                        try
                        {
                            doWork
                                (
                                    delegate
                                        {
                                                olMailItem.Save();  // save mail item here                  
    //intermittent error here:
    //System.Runtime.InteropServices.COMException (0x8004010A): The operation cannot be performed because the object has been deleted.                    
                                        }
                                );
                        }
                        catch (InvalidComObjectException) { }
                        catch (COMException ex)
                        {
    throw;
                        }
                        catch(SEHException e)
                        {
                            throw new ActionIsAbortedException("");
                        }
                }
    ============================

    // class Com
     public static T CloneRef<T>(T source)
            {
                IntPtr iUnknown = Marshal.GetIUnknownForObject(source);
                try
                {
                    return (T)Marshal.GetUniqueObjectForIUnknown(iUnknown);
                }
                finally
                {
                    Marshal.Release(iUnknown);
                }
            }




    • Edited by sergeya Tuesday, November 11, 2014 12:47 AM
    Tuesday, November 11, 2014 12:47 AM

All replies

  • The error is MAPI_E_OBJECT_DELETED, which means the message was deleted while you still had a reference to it and attempted to call Save.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!


    Tuesday, November 11, 2014 1:33 AM
  • What exactly do you need to implement in the ItemSend event handler?

    Most probably there is more elegant way for implementing your task.

    Tuesday, November 11, 2014 3:38 PM