private MailMessageItem SaveToDraft(int tenant_id, string username, MailSendItem item, int mail_id, out string mime_message_id, out string in_reply_to, MailBox mbox) { item.DisplayName = mbox.Name; var embeded_attachments_for_saving = item.ChangeEmbededAttachmentLinksForStoring(tenant_id, username, mail_id, _manager); var message_item = item.ToMailMessageItem(tenant_id, username); message_item.IsNew = false; message_item.Folder = MailFolder.Ids.drafts; mime_message_id = mail_id == 0 ? _manager.CreateMessageId() : _manager.GetMimeMessageIdByMessageId(mail_id); in_reply_to = item.ReplyToId != 0 ? _manager.GetMimeMessageIdByMessageId(item.ReplyToId) : ""; if (!string.IsNullOrEmpty(mime_message_id)) { message_item.MessageId = mime_message_id; } if (!string.IsNullOrEmpty(in_reply_to)) { message_item.InReplyTo = in_reply_to; } message_item.ChainId = _manager.DetectChainId(mbox, message_item); var need_to_restore_attachments_from_fck_location = mail_id == 0 && message_item.Attachments.Any(); if (need_to_restore_attachments_from_fck_location) { message_item.Attachments.ForEach(attachment => _manager.StoreAttachmentCopy(tenant_id, username, attachment, item.StreamId)); } _manager.StoreMailBody(mbox.TenantId, mbox.UserId, message_item); var previous_mailbox_id = mail_id == 0 ? mbox.MailBoxId : _manager.GetMailInfo(tenant_id, username, mail_id, false, false).MailboxId; var previous_chain_id = message_item.ChainId; if (previous_mailbox_id != mbox.MailBoxId) { var prev_mbox = new MailBox(); prev_mbox.TenantId = tenant_id; prev_mbox.UserId = username; prev_mbox.MailBoxId = previous_mailbox_id; previous_chain_id = _manager.DetectChainId(prev_mbox, message_item); } mail_id = _manager.MailSave(mbox, message_item, mail_id, message_item.Folder, message_item.Folder, string.Empty, string.Empty, false); message_item.Id = mail_id; if (previous_mailbox_id != mbox.MailBoxId) { _manager.UpdateChain(previous_chain_id, message_item.Folder, previous_mailbox_id, tenant_id, username); } if (mail_id > 0 && need_to_restore_attachments_from_fck_location) { foreach (var attachment in message_item.Attachments) { var new_id = _manager.SaveAttachment(tenant_id, mail_id, attachment); attachment.fileId = new_id; } } if (mail_id > 0 && embeded_attachments_for_saving.Any()) { _manager.SaveAttachments(tenant_id, mail_id, embeded_attachments_for_saving); } _manager.UpdateChain(message_item.ChainId, message_item.Folder, mbox.MailBoxId, mbox.TenantId, mbox.UserId); return message_item; }
private MailMessageItem SaveToDraft(MailSendItem originalMessage, int mailId, MailBox mbox) { originalMessage.DisplayName = mbox.Name; var embededAttachmentsForSaving = originalMessage.ChangeEmbededAttachmentLinksForStoring(mbox.TenantId, mbox.UserId, mailId, manager); var messageItem = originalMessage.ToMailMessageItem(mbox.TenantId, mbox.UserId); messageItem.IsNew = false; messageItem.Folder = MailFolder.Ids.drafts; messageItem.ChainId = messageItem.MimeMessageId; var needToRestoreAttachmentsFromFckLocation = mailId == 0 && messageItem.Attachments.Any(); if (needToRestoreAttachmentsFromFckLocation) { messageItem.Attachments.ForEach(attachment => manager.StoreAttachmentCopy(mbox.TenantId, mbox.UserId, attachment, originalMessage.StreamId)); } manager.StoreMailBody(mbox.TenantId, mbox.UserId, messageItem); var previousMailboxId = mailId == 0 ? mbox.MailBoxId : manager.GetMailInfo(mbox.TenantId, mbox.UserId, mailId, false, false).MailboxId; mailId = manager.MailSave(mbox, messageItem, mailId, messageItem.Folder, messageItem.Folder, string.Empty, string.Empty, false); messageItem.Id = mailId; if (previousMailboxId != mbox.MailBoxId) { manager.UpdateChain(messageItem.ChainId, messageItem.Folder, previousMailboxId, mbox.TenantId, mbox.UserId); } if (mailId > 0 && needToRestoreAttachmentsFromFckLocation) { foreach (var attachment in messageItem.Attachments) { var newId = manager.SaveAttachment(mbox.TenantId, mailId, attachment); attachment.fileId = newId; } } if (mailId > 0 && embededAttachmentsForSaving.Any()) { manager.SaveAttachments(mbox.TenantId, mailId, embededAttachmentsForSaving); } manager.UpdateChain(messageItem.ChainId, messageItem.Folder, mbox.MailBoxId, mbox.TenantId, mbox.UserId); if (previousMailboxId != mbox.MailBoxId) manager.UpdateCrmLinkedMailboxId(messageItem.ChainId, mbox.TenantId, previousMailboxId, mbox.MailBoxId); return messageItem; }