Including Attachments with Cloud Mail


Requirements: Cloud Mail

Cloud Mail provides a suite of easy-to-use mail management components. This article covers including various types of attachments when sending emails with Amazon's Simple Email Service (SES), Google's Gmail, and Microsoft's Office 365 (Graph API). For more about Cloud Mail's capabilities as a whole, please see our Getting Started with Cloud Mail article for more information.

Contents

Amazon SES

The AmazonSES component supports adding multiple types of attachments in several different ways. Some of the most common are shown below.

File Attachments

The simplest way to include a file attachment is the AddAttachment method, which opens the file and encodes its contents in MIME format. Once the attachments have been added, call the Send method to send the message.

amazonses.From = "verified.sender@example.com";
amazonses.SendTo = "recipient@example.com";
amazonses.Subject = "Monthly Reports";
amazonses.MessageText = "The monthly reports are attached.";
// Attach two files.
amazonses.AddAttachment("C:\\reports\\file1.zip");
amazonses.AddAttachment("C:\\reports\\file2.zip");
// Send the message.
amazonses.Send();

Alternatively, in editions that support collections and types, attachments can be added directly to the Attachments collection. The Attachments property is a collection of FileAttachment objects, an included type that describes the file being attached.

amazonses.Attachments.Add(new FileAttachment("report.txt", "C:\\reports\\file.txt"));

In editions without support for collections and types, a set of indexed properties can be used to achieve the same result. The exact syntax for these properties depends on the edition.

amazonses.AttachmentCount = 1;
amazonses.AttachmentName(0) = "report.txt";
amazonses.AttachmentFileName(0) = "C:\\reports\\file.txt";

Attachments from Streams

In editions that support streams, the data for an attachment can also be supplied through a stream with the SetAttachmentInStream method. The first parameter is the index of the attachment in the Attachments collection whose data the stream will supply. When multiple data sources are provided for an attachment, the stream takes priority, followed by the file path, and finally the raw data.

amazonses.Attachments.Add(new FileAttachment("report.txt"));
amazonses.SetAttachmentInStream(0, new FileStream("C:\\reports\\file.txt", FileMode.Open));

Embedded Image Attachments

The AmazonSES component also supports embedded image attachments via the Images collection, which is used in conjunction with the MessageHTML property. The Images property is a collection of HTMLImage objects, an included type that describes the image being embedded. Each image's Id must match the Content-ID referenced by a cid: URL in the HTML message.

amazonses.MessageHTML = "<b>Embedded Image:</b><br /><img src='cid:1444'>";
HTMLImage image = new HTMLImage("C:\\images\\image.bmp");
image.Id = "1444";
image.ImageType = "image/bmp";
amazonses.Images.Add(image);

In editions without support for collections and types, this can be achieved with the equivalent indexed properties.

amazonses.MessageHTML = "<b>Embedded Image:</b><br /><img src='cid:1444'>";
amazonses.ImageCount = 1;
amazonses.ImageFile(0) = "C:\\images\\image.bmp";
amazonses.ImageId(0) = "1444";
amazonses.ImageType(0) = "image/bmp";

Gmail

The Gmail component supports adding file attachments to new messages.

File Attachments

File attachments are specified as a semicolon-separated list of file paths in the MessageAttachments property. These files are attached to messages sent with the SendMail method, as well as drafts created with the CreateDraft method.

gmail.MessageTo = "recipient@example.com";
gmail.MessageSubject = "Monthly Reports";
gmail.MessageBodyContentType = "text";
gmail.MessageBodyContent = "The monthly reports are attached.";
// Attach two files.
gmail.MessageAttachments = "C:\\reports\\file1.zip;C:\\reports\\file2.zip";
// Send the message.
gmail.SendMail();

Office365

The Office365 component supports adding multiple types of attachments in several different ways. Some of the most common are shown below.

File Attachments

Attachments for new messages are added to the MessageAttachments collection. These attachments are included in messages sent with the SendMail method, as well as drafts created with the CreateDraft method using the DRAFTTYPE_MESSAGE (0) draft type. The MessageAttachments property is a collection of OLAttachment objects, an included type that describes the file being attached.

office365.MessageTo = "recipient@example.com";
office365.MessageSubject = "Monthly Reports";
office365.MessageBodyContentType = "text";
office365.MessageBodyContent = "The monthly reports are attached.";
// Attach a file.
office365.MessageAttachments.Add(new OLAttachment("report.txt", "C:\\reports\\file.txt"));
// Send the message and save a copy to the Sent Items folder.
office365.SendMail(true);

In editions without support for collections and types, a set of indexed properties can be used to achieve the same result. The exact syntax for these properties depends on the edition.

office365.MessageAttachmentCount = 1;
office365.MessageAttachmentName(0) = "report.txt";
office365.MessageAttachmentFileName(0) = "C:\\reports\\file.txt";

In editions that support streams, the data for an attachment can also be supplied through a stream with the SetAttachmentInStream method. The first parameter is the index of the attachment in the MessageAttachments collection whose data the stream will supply.

office365.MessageAttachments.Add(new OLAttachment("report.txt", ""));
office365.SetAttachmentInStream(0, new FileStream("C:\\reports\\file.txt", FileMode.Open));

Attachments on Existing Drafts

The AddAttachment method adds a file attachment to an existing message, such as a draft. The first parameter is the ID of the message, the second is the name of the attachment, and the third is the path to the local file. Files larger than the AttachmentSimpleUploadLimit configuration setting (2 MiB by default) are automatically uploaded in fragments using an upload session, with the fragment size controlled by the AttachmentFragmentSize configuration setting. Note that AddAttachment is not used for messages sent with the SendMail method.

// Create a draft and add two attachments to it.
office365.CreateDraft(0, "");
string messageId = office365.MessageInfo[0].Id;
office365.AddAttachment(messageId, "file1.zip", "C:\\reports\\file1.zip");
office365.AddAttachment(messageId, "file2.zip", "C:\\reports\\file2.zip");
// Send the draft.
office365.SendDraft(messageId);

To add a more complex attachment to an existing draft (e.g., an inline attachment or one supplied through a stream), add it to the MessageAttachments collection, and then call AddAttachment with empty strings for the name and file parameters. The component will then use the first attachment in the MessageAttachments collection.

Inline Attachments

The Office365 component also supports inline attachments, such as images displayed within the body of an HTML message. To include an inline attachment, set the attachment's IsInline field to true and its ContentId field to the Content-ID referenced by a cid: URL in the message body.

office365.MessageBodyContentType = "html";
office365.MessageBodyContent = "<b>Inline Attachment:</b><br /><img src='cid:inline1'>";
office365.MessageAttachments.Add(new OLAttachment("logo.png", "C:\\images\\logo.png"));
office365.MessageAttachments[0].IsInline = true;
office365.MessageAttachments[0].ContentId = "inline1";

In editions without support for collections and types, this can be achieved with the equivalent indexed properties.

office365.MessageBodyContentType = "html";
office365.MessageBodyContent = "<b>Inline Attachment:</b><br /><img src='cid:inline1'>";
office365.MessageAttachmentCount = 1;
office365.MessageAttachmentName(0) = "logo.png";
office365.MessageAttachmentFileName(0) = "C:\\images\\logo.png";
office365.MessageAttachmentIsInline(0) = true;
office365.MessageAttachmentContentId(0) = "inline1";

Item Attachments

The Office365 component also supports item attachments, which are non-file attachments such as other Outlook messages, events, and contacts. These attachments are added to an existing message (such as a draft) using the AddItemAttachment method. This method accepts either the ID of a message to attach, or raw item JSON describing an event or contact to attach.

// Attach another message by specifying its ID.
office365.AddItemAttachment(messageId, "AAMkADNkNJp5JVnQIe9r0=", "");
// Attach an event by specifying its raw JSON instead.
office365.AddItemAttachment(messageId, "", "{\"@odata.type\": \"#microsoft.graph.itemAttachment\", \"name\": \"Holiday event\", \"item\": {\"@odata.type\": \"microsoft.graph.event\", \"subject\": \"Discuss gifts for children\", \"body\": {\"content\": \"Let's look for funding!\"}, \"start\": {\"dateTime\": \"2016-12-02T18:00:00\"}, \"end\": {\"dateTime\": \"2016-12-02T19:00:00\"}}}");

The examples above cover some of the most common ways to add attachments using the Cloud Mail components. Please review the component documentation for more details.

We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.