Create Email activity with Zip file attachment Dynamics 365

We had a requirement to create email activity with zip file as attachment. Using other file type e.g. xlsx, docx as attachment are simple. However, using zip file as attachment is a bit different. Here’s the code to create email activity with zip file as attachment:

</pre>
Entity email = new Entity("email");
email["to"] = toParty; // Entity[] already defined
email["from"] = fromParty; // Entity[] already defined
email["subject"] = "Test Mail";
email["description"] = "Description";
email["directioncode"] = true;
Guid mailRecordId = (Guid)crmOrgService.Create(email);

Entity attachment = new Entity("activitymimeattachment");
attachment["subject"] = "Attachment";
string attachFileName = string.Format("IntegrationErrorLog_{0}.zip", DateTime.UtcNow.ToFileTimeUtc());
attachment["filename"] = attachFileName;

//For docx, xlsx etc. file types
// byte[] fileStream = Encoding.ASCII.GetBytes(File.ReadAllText(fileName));

//For zip file
byte[] fileStream = File.ReadAllBytes(fileName);

attachment["body"] = Convert.ToBase64String(fileStream);

//mimetype is different for  different file types e.g. xlsx, docx
attachment["mimetype"] = "application/zip";
attachment["attachmentnumber"] = 1;
attachment["objectid"] = new EntityReference("email", mailRecordId);
attachment["objecttypecode"] = "email";
crmOrgService.Create(attachment);
<pre>

Hope it helps.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s