Working with Message Attachments

Managing Attachments with Aspose Outlook

Creating and Saving Outlook Message (MSG) Files explained how to create and save messages, and how to create MSG files with attachments. This article explains how to manage Microsoft Outlook attachments with Aspose.Email. Attachments from a message file are accessed and saved to disk using the MapiMessage class Attachments property. The Attachments property is a collection of type MapiAttachmentCollection class.

Save Attachments from Outlook Message (MSG) file

To save attachments from an MSG file:

  1. Iterate through the MapiAttachmentCollection collection and get the individual attachments.
  2. To save the attachments, call the MapiAttachment class Save() method.

The following code snippet shows you how to saves attachments to the local disk.

Getting Nested Mail Message Attachments

Embedded OLE attachments also appear in the MapiMessage class Attachment collection. The following code example parses a message file for embedded message attachments and saves it to disk. The MapiMessage class FromProperties() static method can create a new message from embedded attachment. The following code snippet shows you how to get nested mail message attachments.

Removing Attachments

Aspose Outlook library provides the functionality to remove attachments from Microsoft Outlook Message (.msg) files:

  • Call the RemoveAttachments() method. It takes the path of the message file as a parameter. It is implemented as a public static method, so you don’t need to instantiate the object.

The following code snippet shows you how to removing Attachments.

You can also call the MapiMessage class static method DestoryAttachment(). It works faster than RemoveAttachment(), because the RemoveAttachment() method parses the message file.

Adding MSG Attachments

An Outlook message can contain other Microsoft Outlook messages in attachments either as regular or embedded messages. The MapiAttachmentCollection provides overloaded members of the Add method to create Outlook messages with both types of attachments.

Embedding Message as Attachment

The following code snippet shows you how to outlook MSG files embedded in a MSG file contains a PR_ATTACH_METHOD whose value equals 5.

Reading Embedded Message from Attachment

The following code snippet shows you how to read embedded message from attachment.

Attachments Insertion and Replacement

Aspose.Email API provides the capability to insert attachments at specific index in the parent message. It also provides the facility to replace contents of an attachment with another message attachment. The following code snippet shows you how to attachments Insertion and Replacement.

Insert at Specific location

Aspose.Email API provides the capability to insert a MSG attachment to a parent MSG using the MapiAttachmentCollection’s Insert method MapiAttachmentCollection Insert(int index, string name, MapiMessage msg). The following code snippet shows you how to insert at specific location.

Replace Attachment Contents

This can be used to replace embedded attachment contents with the new ones using the Replace method. However, it can not be used to insert attachment with PR_ATTACH_NUM = 4(for example) in the collection with collection.Count = 2. The following code snippet shows you how to replace attachment contents.