Working with Message Attachments

Parsing and Saving Attachments

Outlook message files may contain one or more attachments. Aspose.Email lets developers loop through the attachments in an MSG file and save them to disk. This topic describes the process. It also describes how to embed an attachment.

Aspose.Email MapiMessage class is used to load an MSG file from disk and exposes the getAttachments() method which references the MapiAttachment object collection associated with the MSG file. The MapiAttachment object further exposes methods that perform actions on the attachment.

To save attachments in an MSG file to disk with the original name and extension:

  1. Create an instance of the MapiMessage class to load an MSG file using the Load() static method.
  2. Call the MapiRecipient class getAttachments() method to get a reference to the collection of MapiAttachment objects associated with the MSG file.
  3. Loop through the MapiAttachmentCollection to display contents regarding each MapiAttachment object through its public methods.
  4. Call the MapiAttachment class save() method to save attachment to the disk.  

Embedding Messages as Attachments

A Microsoft Outlook message can contain other Microsoft Outlook messages in attachments either as regular messages, described above, or embedded messages. The MapiAttachmentCollection provides overloaded members of the add method for creating Outlook messages with both types of attachments. Outlook MSG files embedded in a MSG file contains a PR_ATTACH_METHOD with the value 5.

Reading an Embedded Message from an Attachment

Attachments MSG 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.

Insert MSG Attachment at Specific Location

Aspose.Email API provides the capability to insert an MSG attachment to a parent MSG using the MapiAttachmentCollection.Insert() method.

Replace Embedded MSG 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.

Save Attachments from Digitally Signed Message

Aspose.Email API provides the capability to get or set a value indicating whether clear-signed message will be decoded.