Working with Message Attachments

Managing Attachments with Aspose Outlook

Creating and Saving Outlook Message (MSG) Files explains 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 save attachments to the local disk.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Create an instance of MapiMessage from file
MapiMessage message = MapiMessage.FromFile(dataDir + fileName);
// Iterate through the attachments collection
foreach (MapiAttachment attachment in message.Attachments)
{
// Save the individual attachment
attachment.Save(dataDir + attachment.FileName);
}

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 the 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.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Create a MapiMessage object from the individual attachment
MapiMessage getAttachment = MapiMessage.FromProperties(attachment.ObjectData.Properties);
// Create object of type MailMessageImterpretor from the above message and Save the embedded message to file at disk
MailMessage mailMessage = getAttachment.ToMailMessage(new MailConversionOptions());
mailMessage.Save(dataDir + @"NestedMailMessageAttachments_out.eml", SaveOptions.DefaultEml);

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.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
MapiMessage.RemoveAttachments(dataDir + "AttachmentsToRemove_out.msg");

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

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
MapiMessage.DestroyAttachments(dataDir + "AttachmentsToDestroy_out.msg");

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 a Message as Attachment

The following code snippet shows you how to embed an MSG file attachment to a message.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
MapiMessage message = new MapiMessage("from@test.com", "to@test.com", "Subj", "This is a message body");
MapiMessage attachMsg = MapiMessage.FromFile(dataDir + "Message.msg");
message.Attachments.Add("Weekly report.msg", attachMsg);
message.Save(dataDir + "WithEmbeddedMsg_out.msg");

Reading Embedded Messages from Attachments

The following code snippet shows you how to read embedded messages from attachments.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
var message = MapiMessage.FromFile(fileName);
if (message.Attachments[0].ObjectData.IsOutlookMessage)
{
var getData = message.Attachments[0].ObjectData.ToMapiMessage();
}

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 insert and replace attachments.

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 an attachment at a specific location.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
var message = MapiMessage.FromFile(fileName);
var memoryStream = new MemoryStream();
message.Attachments[2].Save(memoryStream);
var getData = MapiMessage.FromStream(memoryStream);
message.Attachments.Insert(1, "new 11", getData);

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.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
var message = MapiMessage.FromFile(fileName);
var memeoryStream = new MemoryStream();
message.Attachments[2].Save(memeoryStream);
var getData = MapiMessage.FromStream(memeoryStream);
message.Attachments.Replace(1, "new 1", getData);

Save Attachments from Digitally Signed Messages

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