public void AddRelationshipEvents(MailMessageItem item) { CoreContext.TenantManager.SetCurrentTenant(TenantId); var factory = new DaoFactory(CoreContext.TenantManager.GetCurrentTenant().TenantId, CRMConstants.DatabaseId); foreach (var contact_entity in item.LinkedCrmEntityIds) { switch (contact_entity.Type) { case ChainXCrmContactEntity.EntityTypes.Contact: var crm_contact = factory.GetContactDao().GetByID(contact_entity.Id); CRMSecurity.DemandAccessTo(crm_contact); break; case ChainXCrmContactEntity.EntityTypes.Case: var crm_case = factory.GetCasesDao().GetByID(contact_entity.Id); CRMSecurity.DemandAccessTo(crm_case); break; case ChainXCrmContactEntity.EntityTypes.Opportunity: var crm_opportunity = factory.GetDealDao().GetByID(contact_entity.Id); CRMSecurity.DemandAccessTo(crm_opportunity); break; } var file_ids = StoreAttachmentsToCrm(item, contact_entity); var content_string = GetHistoryContentJson(item); AddRelationshipEventWithCrmApi(item, contact_entity, content_string, file_ids); } }
public abstract void HandleRetrievedMessage(MailBox mailbox, Message message, MailMessageItem message_item, int folder_id, string uidl, string md5_hash, bool unread, int[] tags_ids);
public void ConvertMessageToInternalFormatTest() { var message = ParseMessage(FILE_PATH); var mailMessageItem = new MailMessageItem(message); Assert.IsNotNull(mailMessageItem); }
public abstract void HandleRetrievedMessage(MailBox mailbox, Message message, MailMessageItem message_item, int folder_id, string uidl, string md5_hash, bool unread, int[] tags_ids);
public MailMessageItem ConvertMessageToInternalFormat() { var message = ParseMessage(); var mail_message_item = new MailMessageItem(message); Assert.IsNotNull(mail_message_item); return mail_message_item; }
public void BodyReplaceEmbeddedImages() { var message = ParseMessage(FILE_PATH); var mailMessageItem = new MailMessageItem(message); var htmlBodyWithReplacedEmbedded = MailBoxManager.ReplaceEmbeddedImages(mailMessageItem); Assert.IsNotEmpty(htmlBodyWithReplacedEmbedded); }
public override void HandleRetrievedMessage(MailBox mailbox, Message message , MailMessageItem message_item, int folder_id, string uidl, string md5_hash, bool unread, int[] tags_ids) { if (string.IsNullOrEmpty(mailbox.EMailInFolder)) return; try { foreach (var attachment in message_item.Attachments) { using (var file = AttachmentManager.GetAttachmentStream(attachment)) { log.Debug("EmailInMessageHandler HandleRetrievedMessage file name: {0}, folder id: {1}", file.FileName, mailbox.EMailInFolder); var uploaded_file_id = FilesUploader.UploadToFiles(file.FileStream, file.FileName, attachment.contentType, mailbox.EMailInFolder, new Guid(mailbox.UserId), log); if (uploaded_file_id < 0) { log.Error("EmailInMessageHandler HandleRetrievedMessage uploaded_file_id < 0"); } } } } catch (WebException we) { var status_code = ((HttpWebResponse) we.Response).StatusCode; if (status_code == HttpStatusCode.NotFound || status_code == HttpStatusCode.Forbidden) { MailBoxManager.CreateUploadToDocumentsFailureAlert(mailbox.TenantId, mailbox.UserId, mailbox.MailBoxId, (status_code == HttpStatusCode.NotFound) ? MailBoxManager.UploadToDocumentsErrorType .FolderNotFound : MailBoxManager.UploadToDocumentsErrorType .AccessDenied); MailBoxManager.SetMailboxEmailInFolder(mailbox.TenantId, mailbox.UserId, mailbox.MailBoxId, null); mailbox.EMailInFolder = null; } throw; } }
public static void AddToCrmHistory(MailMessageItem item, CrmContactEntity entity, string contentString, IEnumerable <int> fileIds) { var request = new RestRequest("crm/history.json", Method.POST); request.AddParameter("content", contentString) .AddParameter("categoryId", MAIL_CRM_HISTORY_CATEGORY) .AddParameter("created", new ApiDateTime(item.Date)); var crmEntityType = entity.Type.StringName(); if (crmEntityType == ChainXCrmContactEntity.CrmEntityTypeNames.contact) { request.AddParameter("contactId", entity.Id) .AddParameter("entityId", 0); } else { if (crmEntityType != ChainXCrmContactEntity.CrmEntityTypeNames.Case && crmEntityType != ChainXCrmContactEntity.CrmEntityTypeNames.opportunity) { throw new ArgumentException(String.Format("Invalid crm entity type: {0}", crmEntityType)); } request.AddParameter("contactId", 0) .AddParameter("entityId", entity.Id) .AddParameter("entityType", crmEntityType); } if (fileIds != null) { fileIds.ToList().ForEach( id => request.AddParameter("fileId[]", id)); } var response = Execute(request); if (response.ResponseStatus != ResponseStatus.Completed || (response.StatusCode != HttpStatusCode.Created && response.StatusCode != HttpStatusCode.OK)) { throw new ApiHelperException("Add message to crm history failed.", response.StatusCode, response.Content); } }
public void AddRelationshipEvents(MailMessageItem item) { var factory = new DaoFactory(CoreContext.TenantManager.GetCurrentTenant().TenantId, CRMConstants.DatabaseId); foreach (var contactEntity in item.LinkedCrmEntityIds) { switch (contactEntity.Type) { case ChainXCrmContactEntity.EntityTypes.Contact: var crmContact = factory.GetContactDao().GetByID(contactEntity.Id); CRMSecurity.DemandAccessTo(crmContact); break; case ChainXCrmContactEntity.EntityTypes.Case: var crmCase = factory.GetCasesDao().GetByID(contactEntity.Id); CRMSecurity.DemandAccessTo(crmCase); break; case ChainXCrmContactEntity.EntityTypes.Opportunity: var crmOpportunity = factory.GetDealDao().GetByID(contactEntity.Id); CRMSecurity.DemandAccessTo(crmOpportunity); break; } var fileIds = new List<int>(); foreach (var attachment in item.Attachments.FindAll(attach => !attach.isEmbedded)) { using (var file = AttachmentManager.GetAttachmentStream(attachment)) { var uploadedFileId = ApiHelper.UploadToCrm(file.FileStream, file.FileName, attachment.contentType, contactEntity); if (uploadedFileId > 0) { fileIds.Add(uploadedFileId); } } } var contentString = GetHistoryContentJson(item); ApiHelper.AddToCrmHistory(item, contactEntity, contentString, fileIds); } }
private static string GetHistoryContentJson(MailMessageItem item) { string content_string; var content_struct = new CrmHistoryContent { message_id = item.Id }; var serializer = new DataContractJsonSerializer(typeof (CrmHistoryContent)); using (var stream = new MemoryStream()) { serializer.WriteObject(stream, content_struct); content_string = Encoding.UTF8.GetString(stream.GetCorrectBuffer()); } return content_string; }
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 = ApiHelper.GetAuthCookie(UserId, request_uri_builder.Host); 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); }
private List<int> StoreAttachmentsToCrm(MailMessageItem item, CrmContactEntity entity) { var file_ids = new List<int>(); foreach (var attachment in item.Attachments.FindAll(attach => !attach.isEmbedded)) { using (var file = AttachmentManager.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; }
public override void HandleRetrievedMessage(MailBox mailbox, Message message, MailMessageItem message_item, int folder_id, string uidl, string md5_hash, bool unread, int[] tags_ids) { MailContactsManager.SaveMailContacts(GetDb(), mailbox.TenantId, mailbox.UserId, message, log); }