CRM 3.0 SDK Email Attatchemnt Problem.
I wonder if anyone can help,
I use the following code to create multiple attachments in a CRM email, but when the attachment list holds more than 5 attachments and you try and send the email from within CRM, you get the error: Unexpected Error.
If I then remove attachments from the list to only leave 5 it sends. I have removed attachments in many different orders so it is not one particular attachment that causes the problem.
I am using CRM SDK 3.0.7 and have rollup 2 of the CRM installed.
This is my code:
int ctr = 1;
foreach (FileAttachment FileAttachmentin AttatchedFiles)
{
//Add an attachment
// Create a new Attachment object.
// Attach it to the email.
activitymimeattachment attachment =newactivitymimeattachment();
attachment.activityid =newLookup();
attachment.activityid.Value = emailID;
attachment.activityid.type =EntityName.email.ToString();
attachment.attachmentnumber =newCrmNumber();
attachment.attachmentnumber.Value = ctr;
// Create the Attachment in CRM.
Guid attachmentId = pCrmService.Create(attachment);
// Get a pointer to the file and open up a stream
FileInfo pointer =newFileInfo(FileAttachment.FileName);
FileStream fileStream = pointer.OpenRead();
// Encode the data using base64
byte[] byteData =newbyte[(int)fileStream.Length];
fileStream.Read(byteData, 0, (int)fileStream.Length);
string encodedData = System.Convert.ToBase64String(byteData);
//close the stream.
fileStream.Flush();
fileStream.Close();
// Create the request object to upload the file to CRM.
UploadFromBase64DataActivityMimeAttachmentRequest upload =newUploadFromBase64DataActivityMimeAttachmentRequest();
// Set the properties of the request object.
upload.ActivityMimeAttachmentId = attachmentId;
upload.FileName = FileAttachment.FileName;
upload.MimeType ="application/octet-stream";
upload.Base64Data = encodedData;
// Upload the file to CRM.
UploadFromBase64DataActivityMimeAttachmentResponse uploaded = (UploadFromBase64DataActivityMimeAttachmentResponse)pCrmService.Execute(upload);
ctr++;
}
Thanks
Mark

