Eugene Astafiev

How To: Add an existing Outlook e-mail message as an attachment

In my previous post I have described the Add function of the Attachments class in Outlook. Now I want to show you how to attach an e-mail instead of a regular file. For such a task I created a simple method in the code, called AddMessageAsAttachment. This method attaches a MailItem object to the e-mail message and accepts two parameters: the first is the container e-mail message and the other is an e-mail message which should be attached.

Please note that I pass the olEmbeddeditem value for the third parameter of the Add function. If you remember we used the olByValue value for attaching a text file. So, let’s get to work!

C# and Add-in Express:

private void AddMessageAsAttachment(Outlook.MailItem mailContainer, 
                                    Outlook.MailItem mailToAttach)
{
    Outlook.Attachments attachments = null;
    Outlook.Attachment attachment = null;
    try
    {
        attachments = mailContainer.Attachments;
        attachment = attachments.Add(mailToAttach, 
           Outlook.OlAttachmentType.olEmbeddeditem, 1, "The attached e-mail");
        mailContainer.Save();
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
    finally
    {
        if (attachment != null) Marshal.ReleaseComObject(attachment);
        if (attachments != null) Marshal.ReleaseComObject(attachments);
    }
}

VB.NET and Add-in Express:

Private Sub AddMessageAsAttachment(mailContainer As Outlook.MailItem, 
                                   mailToAttach As Outlook.MailItem)
   Dim attachments As Outlook.Attachments = Nothing
   Dim attachment As Outlook.Attachment = Nothing
   Try
      attachments = mailContainer.Attachments
      attachment = attachments.Add(mailToAttach, _
          Outlook.OlAttachmentType.olEmbeddeditem, 1, "The attached e-mail")
      mailContainer.Save()
   Catch ex As Exception
      System.Windows.Forms.MessageBox.Show(ex.Message)
   Finally
      If Not IsNothing(attachment) Then Marshal.ReleaseComObject(attachment)
      If Not IsNothing(attachments) Then Marshal.ReleaseComObject(attachments)
   End Try
End Sub

C# and VSTO:

using System.Runtime.InteropServices;
// ...
private void AddMessageAsAttachment(Outlook.MailItem mailContainer, 
                                    Outlook.MailItem mailToAttach)
{
    Outlook.Attachments attachments = null;
    Outlook.Attachment attachment = null;
    try
    {
        attachments = mailContainer.Attachments;
        attachment = attachments.Add(mailToAttach, 
           Outlook.OlAttachmentType.olEmbeddeditem, 1, "The attached e-mail");
        mailContainer.Save();
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
    finally
    {
        if (attachment != null) Marshal.ReleaseComObject(attachment);
        if (attachments != null) Marshal.ReleaseComObject(attachments);
    }
}

VB.NET and VSTO:

Imports System.Runtime.InteropServices
' ...
Private Sub AddMessageAsAttachment(mailContainer As Outlook.MailItem, _
                                   mailToAttach As Outlook.MailItem)
    Dim attachments As Outlook.Attachments = Nothing
    Dim attachment As Outlook.Attachment = Nothing
    Try
        attachments = mailContainer.Attachments
        attachment = attachments.Add(mailToAttach, _
            Outlook.OlAttachmentType.olEmbeddeditem, 1, "The attached e-mail")
        mailContainer.Save()
    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.Message)
    Finally
        If Not IsNothing(attachment) Then Marshal.ReleaseComObject(attachment)
        If Not IsNothing(attachments) Then Marshal.ReleaseComObject(attachments)
    End Try
End Sub

See you on our forums and in the e-mail support!

12 Comments

  • Dan McCormick says:

    I am using a simpler method than the above example. It seems to work on the Win7 Pro systems I have except one. I have checked the advanced settings in Outlook and the Trust Manager settings. Nothing stands out. I suspect the issue is an Outlook or add-in setting unique to this worksation. A stack trace does not provide any useful detail. No line number are provided. I know which line it is anyway.

    I have a Gridview that nicely presents the inbox. I capture the Entry ID and use it with my attachment code. The error I get on the send when I do an Attachemnt.Add is:

    Outlook Error: System Exception:
    Microsoft.Office.Interop.Outlook.Attachments.Add( … my method constructor …)

    Any thoughts or suggestions on how to better resolve this would be appreciated.

    Thanks, Dan..

  • Eugene Astafiev says:

    Hi Dan,

    Thank you for visiting our technical blog.

    I am sorry, but I can’t give the exact answer off the top of my head, it requires researching. Do you release underlying COM objects properly?

    Please e-mail me to our support address and I will continue to help you with this issue. You can find the support e-mail address in the readme.txt file (it is included in our product packages).

  • Dan McCormick says:

    I resolved this. The issue was that Outlook -> Trust Manager -> settings -> Programmatic Access had to be set to a lower security filter.

    What is not clear, after this resolved the problem, is why it seemed to be required on some of my systems and not others.

    Do you know how to access Outlook security settings with a NET object?

  • Eugene Astafiev says:

    Hi Dan,

    Thank you for keeping me informed.

    Nope. The Outlook Object Model doesn’t provide any property or method for such tasks. Instead, I would recommend contacting your administrator for changing the security settings in Outlook.

  • suki says:

    Hi,
    I’m working on an addin in order to insert an existing outlook item(mails), as attachment. I want to provide to users the possibility to choose an existing message with the insert outlook items dialogue. do you know how to open the insert items dialogue.
    Thanks for help

  • Andrei Smolin (Add-in Express Team) says:

    Hello Suki,

    Here’s a VBA macro:
    Application.ActiveInspector().CommandBars.ExecuteMso “AttachItem”

  • suki says:

    Hi Andrei!

    Thanks for replying. But I’m working on c# and I’m really novice. :/
    I want to open the insert items dialog when the user click on a button.
    thanks

  • Andrei Smolin (Add-in Express Team) says:

    For the statement above, the syntactical differences are minor; Intellisence will help you. The only potential problem is the Addplication entry. In a VBA macro it refers to the Outlook.Application object representing the Outlook application in which the macro is run.

    You need to get an Inspector object representing the window in which you are going to show the “Insert Item” dialog. Typically, this is the active inspector window and thus the need to call ActiveInspector(). Then you retrive the CommandBars collection of that Inspector object – note that Intellisense may not have the “CommandBars” entry on the list, just print it yourself. Finally, you call the ExecuteMso method and pass it the Id of the built-in Ribbon control (which is “AttachItem”).

    Links:
    An entry point:
    https://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook._application.aspx

    The Ids of built-in Ribbon controls in Office can be found here:
    https://www.microsoft.com/en-us/download/details.aspx?id=6627

  • suki says:

    I tried this, but it doesn’t work

    Outlook.Application OutlookApplication = new Outlook.Application();
    OutlookApplication.ActiveInspector().CommandBars.ExecuteMso(“AttachItem”);

  • Andrei Smolin (Add-in Express Team) says:

    The code looks correct. I suppose it doesn’t work because there’s no active inspector in Outlook.

    If you are an Add-in Express customer, please contact us using the contact form at https://www.add-in-express.com/support/askus.php and I’ll create a working example for you.

  • suki says:

    Hi,
    If I understand what you said, I need to use this: Outlook._Application ? I think it is the Application interface in which the addins is run. I tried something like that:
    Outlook.Application oApp = new Outlook.Application();
    Outlook._Application OutlookApplication = (Outlook._Application)oApp….;
    (what should I do here?)
    OutlookApplication.ActiveInspector().CommandBars.ExecuteMso(“AttachItem”);
    Please your help is really important
    Thanks

  • Dmitry Kostochko (Add-in Express Team) says:

    Hello Suki,

    Yes, you are right, you need to use the Application interface that is passed to your add-in. But you need to execute the Application.ActiveInspector().CommandBars.ExecuteMso(“AttachItem”); code line when Outlook has an opened inspector window, e.g. in the Click event handler of a ribbon button.

Post a comment

Have any questions? Ask us right now!