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. 

Rename an Attachment in a MapiMessage

Aspose.Email makes it possible to edit the DisplayName property value in MapiMessage attachments.

The following code sample demonstrates how to update the display names of the first and second attachments within the loaded Mapi message:

MapiMessage msg = MapiMessage.load(fileName);
msg.getAttachments().get_Item(0).setDisplayName("New display name 1");
msg.getAttachments().get_Item(1).setDisplayName("New display name 2");

Check if an Attachment is Inline or Regular

The difference between inline and regular attachments is how they are presented within an email. Inline attachments are embedded within the body of the email and can be viewed without having to open a separate file or download anything. Regular attachments, on the other hand, are separate files that are attached to the email but are not displayed directly within the body of the message and need to be downloaded and opened externally. The MapiAttachment.IsInline property of the MapiAttachment class gets a value indicating whether the attachment is inline or regular.

The following code sample loads an email message from a file and then retrieves information about the attachments, specifically printing the display name of each attachment and whether it is inline within the message or not:

MapiMessage message = MapiMessage.load("fileName");

for (MapiAttachment attach : message.getAttachments()) {
    System.out.println(attach.getDisplayName() + ": " + attach.isInline());