private string PrepareBrowserContent(string htmlBody) { // Clear the files in temporary attachment storage. DirectoryInfo di = new DirectoryInfo(attachmentDirPath); foreach (FileInfo file in di.GetFiles()) { file.Delete(); } foreach (DirectoryInfo dir in di.GetDirectories()) { dir.Delete(true); } // Save all attachments and obtain their <cid, filePath> pairs. this.savedAttachments.Clear(); MimeUtility.SaveAttachments(Message.Body, attachmentDirPath, this.savedAttachments); HtmlDocument html = new HtmlDocument(); html.LoadHtml(htmlBody); var rootNode = html.DocumentNode; var imgNodes = rootNode.Descendants("img"); // Fix all cid references with corresponding file system paths. foreach (HtmlNode node in imgNodes) { string srcAttr = node.GetAttributeValue("src", null); if (srcAttr != null) { Match m = Regex.Match(srcAttr, "cid:(.*)"); if (m.Success) { string cid = m.Groups[1].Value.Trim(' '); if (this.savedAttachments.ContainsKey(cid)) { node.SetAttributeValue("src", this.savedAttachments[cid]); } } } } return(rootNode.InnerHtml); }
private void LoadAttachments(string mimeSource) { // Clear the files in temporary storage. DirectoryInfo di = new DirectoryInfo(attachmentDirPath); foreach (FileInfo file in di.GetFiles()) { file.Delete(); } foreach (DirectoryInfo dir in di.GetDirectories()) { dir.Delete(true); } this.savedAttachments.Clear(); MimeUtility.SaveAttachments(mimeSource, attachmentDirPath, this.savedAttachments); Attachments.Clear(); foreach (string path in this.savedAttachments.Values) { Attachments.Add(new AttachmentViewModel(path)); } }