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 Office365 (Graph API). For more about Cloud Mail's capabilities as a whole, please see Getting Started with Cloud Mail.
Contents
AmazonSES
The AmazonSES component supports adding multiple types of attachments in several different ways. Some of the most common are shown below.
File Attachments
The first option for including a file attachment is via the convenient AddAttachment method, which will open the file and encode its contents in MIME format. This method can be used as shown below.
amazonses.AddAttachment("C:\file1.zip");
amazonses.AddAttachment("C:\file2.zip");
Alternatively, in most editions that support collections and types, attachments can be included with the email message by adding them directly to the Attachments collection as shown below.
amazonses.Attachments.Add(new nsoftware.CloudMail.FileAttachment("name", "C:\file.txt"));
Note that the Attachments property is a collection of FileAttachments, an included type that describes the file being attached.
In editions without support for collections and types, several similar properties exist that can be used to achieve the same result, which is demonstrated below.
amazonses.AttachmentCount = 1;
amazonses.AttachmentName(0) = "name";
amazonses.AttachmentFile(0) = "C:\file.txt";
Embedded Image Attachments
The AmazonSES component also includes support for embedded image attachments via the Images collection, which can be used in conjunction with the HTMLMessage property as shown below.
amazonses.MessageHTML = "<b>Embedded Image:</b><br /><img src='cid:1444'></img>";
HTMLImage image = new HTMLImage("C:\image.bmp");
image.Id = "1444";
image.ImageType = "image/bmp";
amazonses.Images.Add(image);
Note that the Images property is a collection of HTMLImages, an included type that describes the image being embedded.
In editions without support for collections and types, this can be achieved as demonstrated below.
amazonses.MessageHTML = "<b>Embedded Image:</b><br /><img src='cid:1444'></img>";
amazonses.ImageCount = 1;
amazonses.ImageFile(0) = "C:\image.bmp";
amazonses.ImageId(0) = "1444";
amazonses.ImageType(0) = "image/bmp";
The above examples cover some of the most common ways to add attachments using the AmazonSES component, but note that in some cases it is possible to add attachments via stream or by supplying the raw data, as well. Please review the component documentation for more details.
Gmail
The Gmail component contains support for adding file attachments.
File Attachments
File attachments can be added in the form of a semicolon separated list to the MessageAttachments property as shown below.
gmail.MessageAttachments = "C:\file1.zip;C:\file2.zip";
Office 365
The Office365 component supports adding multiple types of attachments in several different ways. Some of the most common are shown below.
File Attachments
A file attachment can be added to an existing message (i.e., a draft) by using the convenient AddAttachment method as shown below.
office365.AddAttachment("messageId", "name1", "C:\file1.zip");
office365.AddAttachment("messageId", "name2", "C:\file2.zip");
Alternatively, when using an edition that supports collections and types, attachments can be added directly to the MessageAttachments collection as shown below.
office365.MessageAttachments.Add(new nsoftware.CloudMail.OLAttachment("name", "C:\file.txt"));
Note that the MessageAttachments property is a collection of OLAttachments, an included type that describes the file being attached.
In editions without support for collections and types, several similar properties exist that can be used to achieve the same result, which is demonstrated below.
office365.MessageAttachmentCount = 1;
office365.MessageAttachmentName(0) = "name";
office365.MessageAttachmentFileName(0) = "C:\file.txt";
The MessageAttachments collection (or equivalent properties in applicable editions) can be used to add attachments to both existing message drafts and brand new messages.
Inline Attachments
The Office365 component also includes support for inline attachments. In editions that support types, this can be achieved via the MessageAttachments collection, which can be used in conjunction with the MessageBodyContent and MessageBodyContentType properties as shown below.
office365.MessageBodyContentType = "html";
office365.MessageBodyContent = "<b>Inline Attachment:</b><br /><img src='cid:inline1'></img>";
office365.MessageAttachments.Add(new nsoftware.CloudMail.OLAttachment("name", "C:\file.txt"));
office365.MessageAttachments[0].IsInline = true;
office365.MessageAttachments[0].ContentId = "inline1";
In editions without support for collections and types, this can be achieved as demonstrated below.
office365.MessageBodyContentType = "html";
office365.MessageBodyContent = "<b>Inline Attachment:</b><br /><img src='cid:inline1'></img>";
office365.MessageAttachmentCount = 1;
office365.MessageAttachmentName(0) = "name";
office365.MessageAttachmentFileName(0) = "C:\file.txt";
office365.MessageAttachmentIsInline(0) = true;
office365.MessageAttachmentContentId(0) = "inline1";
Item Attachments
The Office365 component also includes support for item attachments, which are non-file attachments such as other Outlook messages, events, and contacts. These types of attachments can be added to an existing message (i.e., a draft) by using the AddItemAttachment method, which can accept either a message identifier (representing the message to be attached) or raw item JSON (used for attaching an event or contact) as valid parameters. Examples for both types are shown below.
// Include another message as an attachment by specifying its message identifier.
office365.AddItemAttachment("messageId", "AAMkADNkNJp5JVnQIe9r0=", "");
// Include an event as an attachment by specifying the 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 above examples cover some of the most common ways to add attachments using the Office365 component, but note that in some cases it is possible to add attachments via stream or by supplying the raw data, as well. 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.