Managing Message Attachments
Handling Attachments in 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.
Check Attachment Type (Inline or Regular)
Inline and regular attachments serve different purposes. Inline attachments are visually integrated into the email message and are typically images or media files. Meanwhile, regular attachments are separate files attached to the email and can include various types of files. The MapiAttachment.IsInline property of the MapiAttachment class gets a value indicating whether the attachment is inline or regular.
The following code sample extracts and displays information about each attachment in the loaded MapiMessage, including their display names and whether they are inline attachments or not.
var message = MapiMessage.Load(fileName);
foreach (var attach in message.Attachments)
{
Console.WriteLine($"{attach.DisplayName0} : {attach.IsInline)}");
}
Check Attachment Type (IsReference)
The MapiAttachment class includes the IsReference property which allows developers to identify reference attachments in a message. With thefollowing code sample, you can check if an attachment is a reference attachment:
foreach (var attachment in msg.Attachments)
{
if (attachment.IsReference)
{
// Process reference attachment
}
}
Save Attachments from MSG Files
To save attachments from an MSG file:
- Iterate through the MapiAttachmentCollection collection and get the individual attachments.
- To save the attachments, call the MapiAttachment class Save() method.
The following code snippet shows you how to save attachments to the local disk.
Extract Attachments from RTF-Formatted MSG Files
For messages formatted as RTF, the following code can be used to differentiate and extract attachments that are either Inline or appear as Icon in the message body. The following code snippet shows you how to Identify and Extract an embedded attachment from MSG formatted as RTF.
var eml = MapiMessage.Load("MSG file with RTF Formatting.msg");
foreach (var attachment in eml.Attachments)
{
if (IsAttachmentInline(attachment))
{
try
{
SaveAttachment(attachment, Data.Out/new Guid().ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
static bool IsAttachmentInline(MapiAttachment attachment)
{
foreach (var property in attachment.ObjectData.Properties.Values)
{
if (property.Name == "\x0003ObjInfo")
{
var odtPersist1 = BitConverter.ToUInt16(property.Data, 0);
return (odtPersist1 & (1 << (7 - 1))) == 0;
}
}
return false;
}
static void SaveAttachment(MapiAttachment attachment, string fileName)
{
foreach (var property in attachment.ObjectData.Properties.Values)
{
if (property.Name == "Package")
{
using var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
fs.Write(property.Data, 0, property.Data.Length);
}
}
}
Get 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.
Remove 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.
Add 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.
Try it out!
Add or remove email attachments with the free Aspose.Email Editor App.
Add Reference Attachments to MapiMessages
The ReferenceAttachmentOptions class simplifies the addition of reference attachments by encapsulating all necessary properties in a single object.
Parameters of ReferenceAttachmentOptions:
- sharedLink: A fully qualified shared link to the attachment provided by the web service hosting the file.
- url: The file location or resource URL.
- providerName: The name of the reference attachment provider (e.g., Google Drive, Dropbox).
- Example: Adding a Reference Attachment with ReferenceAttachmentOptions
var options = new ReferenceAttachmentOptions(
"https://drive.google.com/file/d/1HJ-M3F2qq1oRrTZ2GZhUdErJNy2CT3DF/",
"https://drive.google.com/drive/my-drive",
"GoogleDrive");
// Add reference attachment
msg.Attachments.Add("Document.pdf", options);
Embed Messages as Attachments
The following code snippet shows you how to embed an MSG file attachment to a message.
Read Embedded Messages from Attachments
The following code snippet shows you how to read embedded messages from attachments.
Inserting and Replacing Attachment
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.
Try it out!
Run the ReplaceAttach simple app project, and try the Aspose.Email capabilities to replace attachments in action.
Insert Attachments at Specific Locations
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.
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.
Rename Attachments in MapiMessage
It is possible to edit the DisplayName property value in MapiMessage attachments.
var msg = MapiMessage.Load(fileName);
msg.Attachments[0].DisplayName = "New display name 1";
msg.Attachments[1].DisplayName = "New display name 2";
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.