Working with Attachments and Embedded Objects

Managing Email Attachments

An email attachment is a file that 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. 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 an 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 an Attachment

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

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

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

// Create an instance of MailMessage class
var eml = new MailMessage
{
    From = "sender@from.com",
    To = "receiver@to.com",
    Subject = "This is message",
    Body = "This is body"
};

// Load an attachment
var attachment = new Attachment("1.txt");

// Add Multiple Attachment in instance of MailMessage class and Save message to disk
eml.Attachments.Add(attachment);

eml.AddAttachment(new Attachment("1.jpg"));
eml.AddAttachment(new Attachment("1.doc"));
eml.AddAttachment(new Attachment("1.rar"));
eml.AddAttachment(new Attachment("1.pdf"));
eml.Save("AddAttachments.eml");

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.

Adding a Reference Attachment

A reference attachment is a type of attachment that includes a link or a reference to a file or item, rather than including the file or item itself in the email message. When the recipients of the email click on the reference attachment, they will be able to access the linked file if they have the appropriate permissions to do so. By using a reference attachment, you can send a smaller email message and ensure that everyone has access to the most up-to-date version of the file or item.

The code snippet below shows how to add a reference attachment to an email. The code performs the following steps:

  1. Loads the email message file using the MailMessage.Load() method.
  2. Creates a new ReferenceAttachment object called refAttach, passing the attachment URL “https://[attach_uri]” as a parameter to its constructor.
  3. Sets the name of the attachment to “Document.docx” using the Name property of the refAttach object.
  4. Sets the provider type of the attachment to AttachmentProviderType.OneDrivePro using the ProviderType property of the refAttach object.
  5. Sets the permission type of the attachment to AttachmentPermissionType.AnyoneCanEdit using the PermissionType property of the refAttach object.
  6. Adds the refAttach object to the Attachments collection of the eml object using the Add() method.
var eml = MailMessage.Load("fileName");

var refAttach = new ReferenceAttachment("https://[attach_uri]")
{
    Name = "Document.docx",
    ProviderType = AttachmentProviderType.OneDrivePro,
    PermissionType = AttachmentPermissionType.AnyoneCanEdit
};

eml.Attachments.Add(refAttach);

Removing an Attachment

To remove an attachment, follow the steps given below:

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

// Create an instance of MailMessage class
var eml = new MailMessage {From = "sender@sender.com", To = "receiver@gmail.com"};

// Load an attachment
var attachment = new Attachment("1.txt");
eml.Attachments.Add(attachment);

// Remove attachment from your MailMessage
eml.Attachments.Remove(attachment);

Displaying Attachment File Name

To display an attachment file name, follow these steps:

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

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

var eml = MailMessage.Load("Attachments.eml");

foreach (var attachment in eml.Attachments)
{
    // Display the the attachment file name
    Console.WriteLine(attachment.Name);
}

Extracting Email Attachments

This topic explains how to extract an attachment from an email file. An email attachment is a file that 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 email messages include an option 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.
Extracted attachments in email
todo:image_alt_text
The following code snippet shows you how to extract email attachments.
var eml = MailMessage.Load("Message.eml", new MsgLoadOptions());

foreach (var attachment in eml.Attachments)
{
    attachment.Save("MessageEmbedded_out.eml");
    Console.WriteLine(attachment.Name);
}

Retrieving Content-Description from Attachment

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

var eml = MailMessage.Load("EmailWithAttachEmbedded.eml");
Console.WriteLine(eml.Attachments[0].Headers["Content-Description"]);

Determining if the Attachment is an Embedded Message

The following code snippet demonstrates how to determine if the attachment is an embedded message or not.

var eml = MailMessage.Load("EmailWithAttachEmbedded.eml");

Console.WriteLine(eml.Attachments[0].IsEmbeddedMessage
    ? "Attachment is an embedded message."
    : "Attachment isn't an embedded message.");

Working with inline images

Add inline image to email body

The LinkedResource class is used with the MailMessage class to embed objects in your email message. 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 LinkedResource class.
  5. Load an embedded object into the LinkedResourceCollection.
  6. Add the loaded embedded object into the MailMessage class instance.
  7. Add the AlternateView 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 than 10MB. The code snippets below demonstrate how to embed objects into an Email.
var eml = new MailMessage
{
    From = "AndrewIrwin@from.com",
    To = "SusanMarc@to.com",
    Subject = "This is an email"
};

// Create the plain text part It is viewable by those clients that don't support HTML
var plainView =
    AlternateView.CreateAlternateViewFromString("This is my plain text content", null, "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'.
var htmlView =
    AlternateView.CreateAlternateViewFromString("Here is an embedded image.<img src=cid:barcode>", null,
        "text/html");

// Create the LinkedResource (embedded image) and Add the LinkedResource to the appropriate view
var barcode = new LinkedResource("1.jpg", MediaTypeNames.Image.Jpeg)
{
    ContentId = "barcode"
};

eml.LinkedResources.Add(barcode);
eml.AlternateViews.Add(plainView);
eml.AlternateViews.Add(htmlView);

eml.Save("EmbeddedImage_out.msg", SaveOptions.DefaultMsgUnicode);

Remove inline image from email body

LinkedResourceCollection accessed via MailMessage.LinkedResources property. The LinkedResourceCollection collection provides a method to completely remove embedded objects added into an email message. Use the overloaded version of LinkedResourceCollection.RemoveAt method to remove all traces of an embedded object from an email message.

The sample code below shows how to remove embedded objects from an email message.

//Load the test message with Linked Resources
var eml = MailMessage.Load("EmlWithLinkedResources.eml");

//Remove a LinkedResource
eml.LinkedResources.RemoveAt(0, true);

//Now clear the Alternate View for linked Resources
eml.AlternateViews[0].LinkedResources.Clear(true);

Working with Embedded Objects

An embedded object is an object that was created with one application and enclosed within a document or a 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.

Extracting Embedded Objects

This topic explains how to extract embedded objects from an email file. The embedded document can be opened in the original application and be 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 the sender and recipient address in the MailMessage instance.
  6. Send email using the SmtpClient class.

The code snippet below extracts embedded objects from an email.

Extracted embedded objects in email
todo:image_alt_text
The following code snippet shows you how to extract embedded objects.
var eml = MailMessage.Load("Message.msg", new MsgLoadOptions());

foreach (var attachment in eml.Attachments)
{
    attachment.Save("MessageEmbedded_out.msg");
    Console.WriteLine(attachment.Name);
}

Identify and Extract an embedded attachment from MSG formatted as RTF

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);
        }
    }
}

Retrieving Attachments from Signed Email

Signed emails contain a single smime.p7m attachment. It means that the email is encrypted by SMIME. Smime.p7m file format is the digital signature. To see the contents of this email use the RemoveSignature method. The method returns a MailMessage object without a digital signature.

var signedEml = MailMessage.Load("signed.eml");
        
if (signedEml.IsSigned)
{
    for (var i = 0; i < signedEml.Attachments.Count; i++)
    {
        Console.WriteLine($@"Signed email attachment{i}: {signedEml.Attachments[i].Name}");
    }
    
    // The email is signed. Remove a signature.
    var eml = signedEml.RemoveSignature();
    
    Console.WriteLine(@"Signature removed.");

    for (var i = 0; i < eml.Attachments.Count; i++)
    {
        Console.WriteLine($@"Email attachment{i}: {eml.Attachments[i].Name}");
    }
}