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’s 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 fromFile() 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 collection to display contents regarding each MapiAttachment object through its public methods.
  4. Call the MapiAttachment class' save() method to save attachment to the disk.  
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
//Instantiate an MSG file to load an MSG file from disk
MapiMessage outlookMessageFile = MapiMessage.fromFile(dataDir + "WithEmbeddedMsg.msg");
//Loop through the attachments collection associated with the MapiMessage object
for (int i = 0; i < outlookMessageFile.getAttachments().size(); i++) {
//Set a reference to the MapiAttachment object
MapiAttachment outlookMessageAttachment = (MapiAttachment) outlookMessageFile.getAttachments().get_Item(i);
//Display attachment type
System.out.println("Att Type : " + outlookMessageAttachment.getMimeTag());
//Display attached file name
System.out.println("File Name : " + outlookMessageAttachment.getLongFileName());
//Save attachment to the disk
outlookMessageAttachment.save(dataDir + outlookMessageAttachment.getDisplayName());
}

Embedding Message 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 collection 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.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
MapiMessage msg = new MapiMessage("from@test.com", "to@test.com", "Subj", "This is a message body");
MapiMessage attachMsg = MapiMessage.fromFile(dataDir + "message.msg");
msg.getAttachments().add("Weekly report", attachMsg);
msg.save(dataDir + "EmbededMessageAsAttachment.msg");

Reading Embedded Message from Attachment

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
MapiMessage mapi = MapiMessage.fromFile(dataDir + "EmbededMessageAsAttachment.msg");
MapiMessage emb = mapi.getAttachments().get_Item(0).getObjectData().toMapiMessage();

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 a MSG attachment to a parent MSG using the MapiAttachmentCollection’s insert method. MapiAttachmentCollection insert(int, String, MapiMessage)

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
MapiMessage msg = MapiMessage.fromFile(dataDir + "WithEmbeddedMsg.msg");
msg.getAttachments().get_Item(0).save(dataDir + "attachment_out.msg");
MapiMessage emb = MapiMessage.fromStream(new FileInputStream(dataDir + "WithEmbeddedMsg.msg"));
msg.getAttachments().insert(1, "new 11", emb);
msg.save(dataDir + "insertMSGAttachment_out.msg");

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.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
MapiMessage msg = MapiMessage.fromFile(dataDir + "insertMSGAttachment_out.msg");
msg.getAttachments().get_Item(0).save(dataDir + "attachment_out.msg");
MapiMessage emb = MapiMessage.fromStream(new FileInputStream(dataDir + "insertMSGAttachment_out.msg"));
msg.getAttachments().replace(1, "new 1", emb);
msg.save(dataDir + "replaceEmbeddedMSGAttachment_out.msg");

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.