Working with Attachments and Embedded Objects using C++ Email Parser Library

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 with the MailMessage class class. 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 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.

Adding Attachment

To attach an attachment to an email, please follow these steps:

  1. Create an instance of the MailMessage class class.
  2. Create an instance of the Attachment class.
  3. Load attachment into the Attachment instance.
  4. Add the Attachment instance into the MailMessage class instance.

The following code snippet shows you how to add an attachment to an email.

For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
// The path to the File directory.
System::String dataDir = RunExamples::GetDataDir_Email();
// Create an instance of MailMessage class
System::SharedPtr<MailMessage> message = System::MakeObject<MailMessage>();
message->set_From(L"sender@sender.com");
message->get_To()->Add(L"receiver@gmail.com");
// Load an attachment
System::SharedPtr<Attachment> attachment = System::MakeObject<Attachment>(dataDir + L"1.txt");
// Add Multiple Attachment in instance of MailMessage class and Save message to disk
message->get_Attachments()->Add(attachment);
message->AddAttachment(System::MakeObject<Attachment>(dataDir + L"1.jpg"));
message->AddAttachment(System::MakeObject<Attachment>(dataDir + L"1.doc"));
message->AddAttachment(System::MakeObject<Attachment>(dataDir + L"1.rar"));
message->AddAttachment(System::MakeObject<Attachment>(dataDir + L"1.pdf"));
message->Save(dataDir + L"outputAttachments_out.msg", SaveOptions::get_DefaultMsgUnicode());

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

Removing an Attachment

To remove an attachment, follow the steps given below:

  • Create an instance of Attachment class.
  • Load attachment in the instance of Attachment class.
  • Add attachment to the instance of MailMessage class.
  • Remove the attachments from the instance of Attachment class using MailMessage class instance.

The following code snippet shows you how to remove an attachment.

For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
// The path to the File directory.
System::String dataDir = RunExamples::GetDataDir_Email();
System::String dstEmailRemoved = dataDir + L"RemoveAttachments.msg";
// Create an instance of MailMessage class
System::SharedPtr<MailMessage> message = System::MakeObject<MailMessage>();
message->set_From(L"sender@sender.com");
message->get_To()->Add(L"receiver@gmail.com");
// Load an attachment
System::SharedPtr<Attachment> attachment = System::MakeObject<Attachment>(dataDir + L"1.txt");
// Add Multiple Attachment in instance of MailMessage class and Save message to disk
message->get_Attachments()->Add(attachment);
message->AddAttachment(System::MakeObject<Attachment>(dataDir + L"1.jpg"));
message->AddAttachment(System::MakeObject<Attachment>(dataDir + L"1.doc"));
message->AddAttachment(System::MakeObject<Attachment>(dataDir + L"1.rar"));
message->AddAttachment(System::MakeObject<Attachment>(dataDir + L"1.pdf"));
// Remove attachment from your MailMessage and Save message to disk after removing a single attachment
message->get_Attachments()->Remove(attachment);
message->Save(dstEmailRemoved, SaveOptions::get_DefaultMsgUnicode());
// Create a loop to display the no. of attachments present in email message
{
auto getAttachment_enumerator = (message->get_Attachments())->GetEnumerator();
decltype(getAttachment_enumerator->get_Current()) getAttachment;
while (getAttachment_enumerator->MoveNext() && (getAttachment = getAttachment_enumerator->get_Current(), true))
{
// Save your attachments here and Display the the attachment file name
getAttachment->Save(dataDir + L"/RemoveAttachments/" + L"attachment_out" + getAttachment->get_Name());
System::Console::WriteLine(getAttachment->get_Name());
}
}

Displaying Attachment File Name

To display the attachment file name, follow these steps:

  1. Loop through the attachments in the email message and
    1. Save each attachment.
    2. Display each attachment’s name on screen.

The following code snippet shows you how to display an attachment file name on the screen.

For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
// Create a loop to display the no. of attachments present in email message
{
auto attachment_enumerator = (message->get_Attachments())->GetEnumerator();
decltype(attachment_enumerator->get_Current()) attachment;
while (attachment_enumerator->MoveNext() && (attachment = attachment_enumerator->get_Current(), true))
{
// Display the the attachment file name
System::Console::WriteLine(attachment->get_Name());
}
}

Extracting Email Attachments

This topic explains how to extract an attachment from an email file. 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. All emails messages includes a body. As well as the body, you might want to send additional files. These are sent as attachments and are represented as instances of the Attachment class. The Attachment class is used with the MailMessage class to work with attachments. To extract attachments from an email message, 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.
  • The code snippets extract attachments from an email.
Extracted attachments in email
todo:image_alt_text
The following code snippet shows you how to Extracting Email Attachments.
For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
// The path to the File directory.
System::String dataDir = RunExamples::GetDataDir_Email();
// Create an instance of MailMessage and load an email file
System::SharedPtr<MailMessage> mailMsg = MailMessage::Load(dataDir + L"Message.msg", System::MakeObject<MsgLoadOptions>());
{
auto attachment_enumerator = (mailMsg->get_Attachments())->GetEnumerator();
decltype(attachment_enumerator->get_Current()) attachment;
while (attachment_enumerator->MoveNext() && (attachment = attachment_enumerator->get_Current(), true))
{
// To display the the attachment file name
attachment->Save(dataDir + L"MessageEmbedded_out.msg");
System::Console::WriteLine(attachment->get_Name());
}
}

Retrieving Content-Description from Attachment

Aspose.Email API provides the capability to read attachment’s Content-Description from attachment header. The following code snippet shows you how to retrieving content description from attachment.

For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
System::SharedPtr<MailMessage> message = MailMessage::Load(dataDir + L"EmailWithAttandEmbedded.eml");
System::String description = message->get_Attachments()->idx_get(0)->get_Headers()->idx_get(L"Content-Description");
System::Console::WriteLine(description);

Working with Embedded Objects

An embedded object is an object that was created with one application an enclosed within a document or file created by another application. For example, a Microsoft Excel spreadsheet can be embedded into a Microsoft Word report, or a video file can be embedded into 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.

Embedding Objects into an Email

The LinkedResources class is used with MailMessage class to embed objects in your email messages. To add an embedded object, follow these steps

  1. Create an instance of the MailMessage class.
  2. Specify the from, to and subject values in MailMessage instance.
  3. Create an instance of the AlternateView class.
  4. Create an instance of the LinkedResources class.
  5. Load an embedded object into the LinkedResources instance.
  6. Add the loaded embedded object into the MailMessage class instance.
  7. Add the AlternateViews instance to the MailMessage class instance.

The code snippets below produce an email message with both plain text and HTML bodies and an image embedded into the HTML

Image embedded into email
todo:image_alt_text
You can send any number of embedded objects. The size of the attachment is limited by the mail server. Gmail, for example, does not support file sizes greater then 10MB. The code snippets below shows how to embed objects into an Email.
For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
// The path to the File directory.
System::String dataDir = RunExamples::GetDataDir_Email();
System::String dstEmail = dataDir + L"EmbeddedImage.msg";
// Create an instance of the MailMessage class and Set the addresses and Set the content
System::SharedPtr<MailMessage> mail = System::MakeObject<MailMessage>();
mail->set_From(System::MakeObject<MailAddress>(L"test001@gmail.com"));
mail->get_To()->Add(L"test001@gmail.com");
mail->set_Subject(L"This is an email");
// Create the plain text part It is viewable by those clients that don't support HTML
System::SharedPtr<AlternateView> plainView = AlternateView::CreateAlternateViewFromString(L"This is my plain text content", nullptr, L"text/plain");
/* Create the HTML part.To embed images, we need to use the prefix 'cid' in the img src value.
The cid value will map to the Content-Id of a Linked resource. Thus <img src='cid:barcode'> will map to a LinkedResource with a ContentId of //'barcode'. */
System::SharedPtr<AlternateView> htmlView = AlternateView::CreateAlternateViewFromString(L"Here is an embedded image.<img src=cid:barcode>", nullptr, L"text/html");
// Create the LinkedResource (embedded image) and Add the LinkedResource to the appropriate view
System::SharedPtr<LinkedResource> barcode = System::MakeObject<LinkedResource>(dataDir + L"1.jpg", MediaTypeNames::Image::Jpeg);
barcode->set_ContentId(L"barcode");
mail->get_LinkedResources()->Add(barcode);
mail->get_AlternateViews()->Add(plainView);
mail->get_AlternateViews()->Add(htmlView);
mail->Save(dataDir + L"EmbeddedImage_out.msg", SaveOptions::get_DefaultMsgUnicode());

Extracting Embedded Objects

This topic explains how to extract embedded objects from an email file. An embedded object is an object that was created with one application an enclosed within a document or file created by another application. For example, a Microsoft Excel spreadsheet can be embedded into a Microsoft Word report, or a video file can be embedded into 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.To extract an embedded object from an email message, follow these steps:

  1. Create an instance of the MailMessage class.
  2. Load an email file in the MailMessage instance.
  3. Create a loop and create an instance of the Attachment class in it.
  4. Save the attachment and display it on screen.
  5. Specify sender and recepient address in the MailMessage instance.
  6. The code snippet below extract embedded objects from an email.
Extracted embedded objects in email
todo:image_alt_text
The following code snippet shows you how to Extracting Embedded Objects.
For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
// The path to the File directory.
System::String dataDir = RunExamples::GetDataDir_Email();
// Create an instance of MailMessage and load an email file
System::SharedPtr<MailMessage> mailMsg = MailMessage::Load(dataDir + L"Message.msg", System::MakeObject<MsgLoadOptions>());
{
auto attachment_enumerator = (mailMsg->get_Attachments())->GetEnumerator();
decltype(attachment_enumerator->get_Current()) attachment;
while (attachment_enumerator->MoveNext() && (attachment = attachment_enumerator->get_Current(), true))
{
// To display the the attachment file name
attachment->Save(dataDir + L"MessageEmbedded_out.msg");
System::Console::WriteLine(attachment->get_Name());
}
}