Working with Attachments and Embedded Objects
Working with Attachments
Email attachments are files sent along with an email message. These files can include documents, images, archives, and more.
In Aspose.Email for C++, attachments are managed through the Attachment class, which works together with the MailMessage class. You can add, remove, extract, or display attachments programmatically.
Note: The maximum attachment size depends on the mail server (for example, Gmail limits attachments to 10 MB).
Try it out!
Add or remove email attachments with the free Aspose.Email Editor App.
Adding Attachments to an Email
To attach one or more files to an email message, follow the steps below:
- Create an instance of the MailMessage class.
- Create Attachment objects for each file.
- Add the attachments to the message using
AddAttachment(). - Save the message if needed.
Code sample
Removing Attachments from an Email
You can easily remove attachments from a message using the Remove() method of the Attachments collection.
The following code snippet demonstrates how to remove an attachment:
- Load or create a MailMessage instance.
- Add one or more attachments.
- Remove the desired attachment.
- Optionally, display remaining attachments.
Displaying Attachment File Names
You can loop through all attachments and display their names on the console:
Extracting Attachments from an Email
To extract and save attachments from an existing email file, follow the steps below:
- Load the message using the MailMessage class.
- Loop through the Attachments collection.
- Save each attachment to disk and optionally display its name.
The following code snippet demonstrates how to extract email attachments:
| Extracted attachments in email |
|---|
![]() |
Reading Attachment Content Description
Aspose.Email allows you to read an attachment’s Content-Description header for additional metadata.
Code sample
Working with Embedded Objects
An embedded object is a file created in one application and enclosed within another document or email. For example, a Microsoft Excel spreadsheet can be embedded in a Word document, or an image can be embedded directly into an email body.
When an object is embedded (not just attached or linked), it retains its original format and can be opened or edited in its native application.
Embedding Objects into an Email
To embed an image or other object in an email message, use the LinkedResource class together with the MailMessage and AlternateView classes. This allows you to include inline images or media content that appears directly in the message body.
The following code sample demonstrates how to create an email message with both plain text and HTML content that includes an embedded image:
- Create an instance of the MailMessage class.
- Set the sender, recipient, and subject fields.
- Create a plain-text body using AlternateView.
- Create an HTML body containing a reference to the embedded object using the
cid(Content-ID). - Create an instance of the LinkedResource class and load the object.
- Assign a Content-ID to the linked resource.
- Add the linked resource and alternate views to the message.
- Save or send the email message.
Note: You can embed multiple objects, but the maximum message size is limited by your mail server. For example, Gmail restricts message size to 10 MB.
Extracting Embedded Objects from an Email
You can also extract embedded objects, such as images or files, from an existing email message. The process is similar to extracting attachments:
- Create an instance of the MailMessage class.
- Load the email file into the MailMessage instance.
- Iterate through the attachments collection.
- Save each embedded object to disk.
- Optionally, display the file names in the console.
| Extracted embedded objects in email |
|---|
![]() |

