internal void AddWrapper(Inspector inspector) { const string SOURCE = CLASS_NAME + "AddWrapper"; dynamic currentItem = null; try { currentItem = inspector.CurrentItem; if (!(currentItem is MailItem)) return; if (currentItem.Sent) return; Logger.Verbose(SOURCE, "creating new wrapper"); var wrapper = new InspWrap(inspector); //don't know how this could happen, but just in case if (_inspWrappers.ContainsKey(wrapper.Key)) { _inspWrappers.Remove(wrapper.Key); } //is this a forward or reply? var item = (MailItem)currentItem; if (!item.Sent && !string.IsNullOrEmpty(item.ConversationIndex) && item.ConversationIndex.Length > 44) { //find the parent var parent = FindParentByConversation(item); if (parent == null) { Logger.Error(SOURCE, string.Format( "failed to retrieve parent message for reply/forward {0}; unable to check for ECS-enabled content", item.Subject)); } else { Logger.Verbose(SOURCE, string.Format( "found parent {0} for reply/forward", parent.Subject)); if (Utils.HasChiaraHeader(parent)) HandleReplyForward(parent, item, ref wrapper); } } Logger.Verbose(SOURCE, "adding wrapper to collection with key " + wrapper.Key); _inspWrappers.Add(wrapper.Key, wrapper); ResetInspButtons(); } catch (Exception ex) { Logger.Error(SOURCE, ex.ToString()); } finally { if (currentItem != null) { Marshal.ReleaseComObject(currentItem); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); } } }
private void HandleReplyForward(MailItem parent, MailItem item, ref InspWrap wrapper) { const string SOURCE = CLASS_NAME + "HandleReplyForward"; try { var sourceContent = string.Empty; var attachList = new List<Attachment>(); GetParentContent(parent, ref sourceContent, ref attachList); if (string.IsNullOrEmpty(sourceContent)) return; //this gets transformed Logger.Verbose(SOURCE, string.Format( "setting SourceContent ({0}), attachList.Count {1} from {2}", sourceContent.Length, attachList.Count, parent.Subject)); wrapper.Dynamic = true; //inherit Encrypted wrapper.Encrypted = !string.IsNullOrEmpty( Utils.GetMailItemHeader(parent, Resources.encrypt_key_header2)); wrapper.Inherited = true; if (parent.BodyFormat == OlBodyFormat.olFormatPlain) { var doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(sourceContent); item.Body = doc.DocumentNode.InnerText.Replace(" ", " "); } else { item.HTMLBody = sourceContent; } if (attachList.Count == 0) return; MAPIUtils mapiUtils = null; try { mapiUtils = RedemptionLoader.new_MAPIUtils(); } catch (Exception ex1) { Logger.Error(SOURCE, "failed to instantiate MAPIUtils object: " + ex1.Message); } if (mapiUtils == null) return; var attachments = item.Attachments; if (attachments.Count.Equals(0)) { Logger.Verbose(SOURCE, "handling as Reply"); //Reply or ReplyAll - we still have to move all the embedded images over object parentProp; try { parentProp = mapiUtils.HrGetOneProp(parent, PR_RECORD_KEY); } catch { parentProp = null; } var parentKey = ""; if (parentProp != null) { parentKey = mapiUtils.HrArrayToString(parentProp); } else { Logger.Warning(SOURCE, "failed to retrieve parentKey"); } var tempPath = Path.Combine(Path.GetTempPath(), "ChiaraMail", parentKey); foreach (var cmAttach in attachList.Where(cmAttach => cmAttach.Type.Equals(1) && !string.IsNullOrEmpty(cmAttach.ContentId))) { Logger.Verbose(SOURCE, "copying over " + cmAttach.Name); //write the bytes to disk (if not there already) var filePath = Path.Combine(tempPath, cmAttach.ContentId); var exists = File.Exists(filePath); if (!exists) File.WriteAllBytes(filePath, cmAttach.Content); //create the attachment var attach = attachments.Add( filePath, Type.Missing, Type.Missing, cmAttach.Name); //set the contentId and hidden props mapiUtils.HrSetOneProp( attach, PR_ATTACH_CONTENT_ID, cmAttach.ContentId, true); mapiUtils.HrSetOneProp( attach, PR_ATTACHMENT_HIDDEN, true, true); //delete the file if it didn't already exist if (!exists) File.Delete(filePath); } } else { Logger.Verbose(SOURCE, string.Format( "handling {0} attachments for Forward", attachList.Count)); //Forward - all the attachments should be there, we just have to update the content foreach (var cmAttach in attachList) { if (!cmAttach.Type.Equals(1)) continue; var attach = attachments[cmAttach.Index]; mapiUtils.HrSetOneProp(attach, PR_ATTACH_DATA_BIN, cmAttach.Content, true); } } } catch (Exception ex) { Logger.Warning(SOURCE, ex.ToString()); } }