Handling Attachments and Embedded Objects
Managing Email Attachments
An email attachment is a computer file which is sent along with an email message. The file may be sent as a separate message as well as a part of the message to which it is attached. The Attachment class is used to manage and manipulate attached files within an email message.
All messages include a body. In addition to the body, you might want to send additional files. These are sent as attachments and are represented as the instance of the Attachment class. You can send any number of attachments but the size of the attachment is limited by the mail server. Gmail, for example, does not support file sizes greater than 10MB.
Try it out!
Add or remove email attachments online with the free Aspose.Email Editor App.
Adding Attachments
To attach a file to an email message, please follow these steps:
- Create an instance of the MailMessage class.
- Create an instance of the Attachment class.
- Load attachment into the Attachment instance.
- Add the Attachment instance into the MailMessage class instance.
The following code snippet shows you how to add an attachment to an email.
Above, we described how to add attachments to your email message with Aspose.Email. What follows shows how to remove attachments, and display information about them on the screen.
Removing Attachments
To remove an attachment, follow the steps given below:
- Create an instance of the MailMessage class.
- Load an attachment from a specified file path using the Attachment class.
- Add the attachment to the MailMessage instance’s attachments collection.
- Print the count of attachments before removing any.
- Remove the attachment from the MailMessage instance’s attachments collection.
- Print the count of attachments after removing the attachment.
The following code snippet shows you how to remove an attachment:
Retrieving Attachment File Names
To display the attachment file name, follow these steps:
- Loop through the attachments in the loaded email.
- For each attachment, print its name using a loop that enumerates the attachments.
The following code snippet shows you how to display an attachment file name on the screen.
Extracting Attachments
In Aspose.Email the MailMessage class is used to work with attachments. An email attachment is a computer file which is sent along with an email message. It may be sent as a separate message as well as a part of the message to which it is attached. The file is represented as an instance of the Attachment class. To extract attachments from email messages, follow these steps:
- Create an instance of the MailMessage class.
- Load an email file into the MailMessage instance.
- Create an instance of the Attachment class and use it in a loop to extract all attachments.
- Save the attachment and display it on screen.
- Specify sender and recepient address in the MailMessage instance.
- Now you can send email using the SmtpClient class.
The following code snippet shows you how to extract email attachments:
| Extracted attachments in email |
|---|
![]() |
Retrieving Content-Description from Attachments
Aspose.Email API provides the capability to read Content-Description from attachment headers. The following code snippet shows you how to retrieve content description from an attachment:
Identifying Embedded Message Attachments
Working with Embedded Objects
An embedded object is an object that was created with one application and embedded in a document or a file created with another application. For example, a Microsoft Excel spreadsheet can be embedded in a Microsoft Word report, or a video file can be embedded in a Microsoft PowerPoint presentation. When a file is embedded, rather than inserted or pasted into another document, it retains its original format. The embedded document can be opened in the original application and modified.
Extracting Embedded Objects
The following code sample demonstrates how to extract embedded objects from an email file using Aspose.Email for Python via .NET.
- Create an instance of the MailMessage class.
- Load an email file in the MailMessage instance.
- Create a loop and an instance of the Attachment class in it.
- Save the attachment and display it on screen.
- Specify sender and recepient address in the MailMessage instance.
- Send email using the SmtpClient class.
| Extracted embedded objects in email |
|---|
![]() |

