'Cloning an outlook email to resend

I want to add a "resend" context menu in my Outlook 2016 add-in, to resend an email. The original email should be re-displayed to the user for him to make any modifications if necessary, and then press the 'send' button. It seems that I need to create a copy of the email, as calling Display() on the original message (or a copy created with MailItem.Copy()) just views the message, as opposed to showing it editable with a send button.

I got this so far - pretty straight forward:

        Outlook.MailItem clone = Globals.ThisAddIn.Application.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
        clone.SendUsingAccount = email.SendUsingAccount;
        clone.To = email.To;
        clone.CC = email.CC;
        clone.BCC = email.BCC;
        clone.Subject = email.Subject;
        clone.Body = email.Body;
        clone.HTMLBody = email.HTMLBody;
        for (int i = 1; i <= email.Attachments.Count; ++i)
            clone.Attachments.Add(email.Attachments[i], email.Attachments[i].Type, email.Attachments[i].Position, email.Attachments[i].DisplayName);

However, I am getting a DISP_E_MEMBERNOTFOUND error when trying to copy the attachments. What am I doing wrong?



Solution 1:[1]

Attachments.Add only allows to pass a string pointing to a fully qualified path to a file or an Outlook item (such as MailItem). Also note that you code only copies the recipient display names, which may or may not be successfully resolved.

Outlook Object Model exposes MailItem.Copy method, but it creates a copy in the same sent/unsent state as the original.

If using Redemption (I am its author) is an option, you can use RDOMail.CopyTo() method - it will copy all the properties and sub-objects (such as recipients and attachments) but it will leave the sent state intact (since in MAPI it can only be set before the message is saved for the very first time).

Off the top of my head:

        using Redemption;
        ...
    
        RDOSession session = new RDOSession();
        session.MAPIOBJECT = Globals.ThisAddIn.Application.Session.MAPIOBJECT;
        RDOMail clone = session.GetDefaultFolder(rdoDefaultFolders.olFolderDrafts).Items.Add();
        RDOMail original = (RDOMail)session.GetRDOObjectFromOutlookObject(email);
        original.CopyTo(clone);
        clone.Save();
        MailItem OutlookClone = Globals.ThisAddIn.Application.Session.GetItemFromID(clone.EntryID);
        OutlookClone.Display()
    
        
    
    
    
    
     

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1