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.
