private void parseMIMEContent(Mail_Message m, string uid, string dir, ASObject record) { XmlDocument doc = new XmlDocument(); XmlElement xml = doc.CreateElement("message"); doc.AppendChild(xml); StringBuilder attachments = new StringBuilder(); MIME_Entity[] entities = m.GetAllEntities(true); Map<string, string> content_id_file = new Map<string, string>(); StringBuilder textHtml = new StringBuilder(); textHtml.Append(@"<html><head><meta http-equiv=""content-type"" content=""text/html; charset=utf-8""></head><body>"); bool hasText = false; foreach (MIME_Entity e in entities) { try { if (e.Body.MediaType.ToLower() == MIME_MediaTypes.Text.html) continue; else if (e.Body.MediaType.ToLower() == MIME_MediaTypes.Text.plain) continue; else if (e.Body is MIME_b_SinglepartBase) { MIME_b_SinglepartBase p = (MIME_b_SinglepartBase)e.Body; Stream data = p.GetDataStream(); string fPath = ""; string fileName = e.ContentType.Param_Name; if (fileName == null) fileName = Guid.NewGuid().ToString(); else attachments.Append(fileName).Append(";"); fileName = fileName.Replace(' ', '_'); fPath = System.IO.Path.Combine(dir, fileName); using (FileStream afs = File.Create(fPath)) { Net_Utils.StreamCopy(data, afs, 4096); } data.Close(); string contentId = e.ContentID; if (!String.IsNullOrEmpty(contentId)) { contentId = contentId.Trim(); if (contentId.StartsWith("<")) contentId = contentId.Substring(1); if (contentId.EndsWith(">")) contentId = contentId.Substring(0, contentId.Length - 1); content_id_file.Add(contentId, fileName); } XmlElement part = doc.CreateElement("PART"); part.SetAttribute("type", "file"); part.SetAttribute("content-id", contentId); part.SetAttribute("filename", fileName); part.SetAttribute("description", e.ContentDescription); if (e.ContentType != null) part.SetAttribute("content-type", e.ContentType.ToString()); xml.AppendChild(part); } } catch (Exception) { } } foreach (MIME_Entity e in entities) { try { if (e.Body.MediaType.ToLower() == MIME_MediaTypes.Text.html) { string html = ((MIME_b_Text)e.Body).Text; //处理html中的内嵌图片 if (content_id_file.Count > 0) { foreach (string key in content_id_file.Keys) { html = html.Replace("cid:" + key, content_id_file[key]); } } XmlElement part = doc.CreateElement("PART"); part.SetAttribute("type", "html"); part.AppendChild(doc.CreateCDataSection(html)); xml.AppendChild(part); string charset = "GBK"; if (e.ContentType != null && e.ContentType.Param_Charset != null) charset = e.ContentType.Param_Charset; else if (m.ContentType != null && m.ContentType.Param_Charset != null) charset = m.ContentType.Param_Charset; string html_charset = getHtmlEncoding(html); if (html_charset == null) { int index = html.IndexOf("<head>", StringComparison.CurrentCultureIgnoreCase); if (index != -1) { StringBuilder sb = new StringBuilder(); index = index + "<head>".Length; sb.Append(html.Substring(0, index)); sb.Append(@"<meta http-equiv=""content-type"" content=""text/html; charset=").Append(charset).Append(@""">"); sb.Append(html.Substring(index)); html = sb.ToString(); } else { StringBuilder sb = new StringBuilder(); sb.Append(@"<html><head><meta http-equiv=""content-type"" content=""text/html; charset=").Append(charset).Append(@""">"); sb.Append("</head><body>"); sb.Append(html); sb.Append("</body></html>"); html = sb.ToString(); } html_charset = charset; } Encoding encoding = null; try { encoding = Encoding.GetEncoding(html_charset); } catch (Exception) { } if (encoding == null) { try { encoding = Encoding.GetEncoding(charset); } catch (Exception) { } } if (encoding == null) encoding = Encoding.UTF8; StreamWriter hfs = new StreamWriter(dir + "/" + uid + ".html", false, encoding); hfs.Write(html); hfs.Close(); } else if (e.Body.MediaType.ToLower() == MIME_MediaTypes.Text.plain) { XmlElement part = doc.CreateElement("PART"); part.SetAttribute("type", "text"); string text = ((MIME_b_Text)e.Body).Text; part.AppendChild(doc.CreateCDataSection(text)); xml.AppendChild(part); if (hasText) textHtml.Append("<hr/>"); hasText = true; text = text.Replace(" ", " "); text = text.Replace("\n", "<br/>"); textHtml.Append(text); } } catch (Exception) { } } textHtml.Append("</body></html>"); if (hasText) { try { using (StreamWriter hfs = new StreamWriter(dir + "/" + uid + ".text.html", false, Encoding.UTF8)) { hfs.Write(textHtml.ToString()); } } catch (Exception) { } } record["attachments"] = attachments.ToString(); record["contents"] = doc.OuterXml; }
private string parseMIMEContent(Mail_Message m, string dir) { MIME_Entity[] entities = m.GetAllEntities(true); if (entities == null) return ""; Map<string, string> content_id_file = new Map<string, string>(); StringBuilder textHtml = new StringBuilder(); foreach (MIME_Entity e in entities) { try { if (e.Body.MediaType.ToLower() == MIME_MediaTypes.Text.html) continue; else if (e.Body.MediaType.ToLower() == MIME_MediaTypes.Text.plain) continue; else if (e.Body is MIME_b_SinglepartBase) { MIME_b_SinglepartBase p = (MIME_b_SinglepartBase)e.Body; string fPath = ""; string fileName = e.ContentType.Param_Name; if (fileName == null) fileName = Guid.NewGuid().ToString(); fileName = fileName.Replace(' ', '_'); fPath = System.IO.Path.Combine(dir, fileName); if (!File.Exists(fPath)) { using (Stream data = p.GetDataStream()) { using (FileStream afs = File.Create(fPath)) { Net_Utils.StreamCopy(data, afs, 4096); } } } string contentId = e.ContentID; if (!String.IsNullOrEmpty(contentId)) { contentId = contentId.Trim(); if (contentId.StartsWith("<")) contentId = contentId.Substring(1); if (contentId.EndsWith(">")) contentId = contentId.Substring(0, contentId.Length - 1); content_id_file.Add(contentId, fPath); } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } } foreach (MIME_Entity e in entities) { try { if (e.Body.MediaType.ToLower() == MIME_MediaTypes.Text.html) { string html = ((MIME_b_Text)e.Body).Text; //处理html中的内嵌图片 if (content_id_file.Count > 0) { foreach (string key in content_id_file.Keys) { html = html.Replace("cid:" + key, content_id_file[key]); } } textHtml.Append(html); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } } return textHtml.ToString(); }