private List<int> StoreAttachmentsToCrm(MailMessageItem item, CrmContactEntity entity)
 {
     var file_ids = new List<int>();
     foreach (var attachment in item.Attachments)
     {
         using (var file = Manager.GetAttachmentStream(attachment))
         {
             var uploaded_file_id = UploadFileToCrm(file.FileStream, file.FileName, attachment.contentType, entity);
             if (uploaded_file_id > 0)
             {
                 file_ids.Add(uploaded_file_id);
             }
         }
     }
     return file_ids;
 }
        private int UploadFileToCrm(Stream file, string filename, string content_type, CrmContactEntity entity)
        {
            var post_parameters = new Dictionary<string, object>
            {
                {"entityType", entity.Type.StringName()},
                {"enitityid", entity.Id},
                {"storeOriginalFileFlag", true}
            };

            var request_uri_builder = GetUploadToCrmUrl(entity.Id, entity.Type.StringName());
            var auth_cookie = GetAuthCookie(request_uri_builder);

            post_parameters.Add("file", new FormUpload.FileParameter(file, filename, content_type));
            var responce = FormUpload.MultipartFormDataPost(request_uri_builder.Uri.ToString(), "", post_parameters, auth_cookie);
            var uploaded_file_id = ParseUploadResponse(responce);
            return uploaded_file_id;
        }
        private void AddRelationshipEventWithCrmApi(MailMessageItem item, CrmContactEntity entity, string content_string, List<int> files_id)
        {
            var body_builder = new StringBuilder();
            body_builder
                .AppendFormat("content={0}", HttpUtility.UrlEncode(content_string))
                .AppendFormat("&categoryId={0}", MailCrmHistoryCategory)
                .AppendFormat("&created={0}", HttpUtility.UrlEncode(new ApiDateTime(item.Date).ToString()));

            var crm_entity_type = entity.Type.StringName();
            if (crm_entity_type == ChainXCrmContactEntity.CrmEntityTypeNames.contact)
            {
                body_builder.AppendFormat("&contactId={0}&entityId=0", entity.Id);
            }
            else
            {
                if (crm_entity_type != ChainXCrmContactEntity.CrmEntityTypeNames.Case
                    && crm_entity_type != ChainXCrmContactEntity.CrmEntityTypeNames.opportunity)
                    throw new ArgumentException(String.Format("Invalid crm entity type: {0}", crm_entity_type));
                body_builder.AppendFormat("&contactId=0&entityId={0}&entityType={1}", entity.Id, crm_entity_type);
            }

            if (files_id != null)
            {
                foreach (var id in files_id)
                {
                    body_builder.AppendFormat("&fileId[]={0}", id);
                }
            }

            var request_uri_builder = GetAddRelationshipEventCrmUrl();
            var auth_cookie = GetAuthCookie(request_uri_builder);

            byte[] body_bytes = Encoding.UTF8.GetBytes(body_builder.ToString());
            var request_stream = new MemoryStream();
            request_stream.Write(body_bytes, 0, body_bytes.Length);
            request_stream.Position = 0;
            FormUpload.PostForm(request_uri_builder.ToString(), "", "application/x-www-form-urlencoded",
                                request_stream, auth_cookie);
        }