public void Open(string filepath = null) { if (filepath == null) { var dialog = new OpenFileDialog { CheckFileExists = true, Filter = "MHT 文件格式(*.mht)|*.mht", InitialDirectory = Directory.GetCurrentDirectory() }; if (dialog.ShowDialog() != DialogResult.OK) { return; } filepath = dialog.FileName; } var file = new FileStream(filepath, FileMode.Open, FileAccess.Read); var reader = new StreamReader(file); string content = reader.ReadToEnd(); MHTContainer mht = MHTContainer.ParseContent(content); MSGContainer msg = MSGContainer.FromMHT(mht); MSG = msg; new MSGFormater().Format(msg); labelMessageTarget.Text = "消息对象: " + msg.To; BindDataList(msg.Messages); }
public static MSGContainer FromMHT(MHTContainer mht) { var container = new MSGContainer(); //container.AssetsFileGetter = mht.GetAssetFile; //container.AssetsGetter = loc => MHTAsset.FromPart(mht.Locations[loc]); // 设置Assets列表 var assets = new Dictionary <string, MHTAsset>(); foreach (var part in mht.Locations) { try { assets[part.Key] = MHTAsset.FromPart(part.Value); } catch { } } container.Assets = assets; string msgs = mht.Parts[1].Content; // 消息部分 // 解析 var reader = new StringReader(msgs); var parser = new MSGMHTParser(reader); while (parser.MoveNext()) { container.Messages.Add(parser.Current); } container.To = parser.To; return(container); }
public static void Main(string[] args) { var file = new FileStream("msg.mht", FileMode.Open, FileAccess.Read); var reader = new StreamReader(file); string content = reader.ReadToEnd(); //var part = MIMEPart.ParseContent(content); MHTContainer mht = MHTContainer.ParseContent(content); MSGContainer msg = MSGContainer.FromMHT(mht); }
public static MHTContainer ParseFile(string filepath) { var mht = new MHTContainer(); var reader = new StreamReader(filepath); return(mht); }
public static MHTContainer ParseContent(string content) { var mht = new MHTContainer(); // 解析 Header 部分 mht.HeaderPart = MIMEPart.ParseHeader(content); content = content.Substring(mht.HeaderPart.HeaderLength + 4); mht.Parts.Add(mht.HeaderPart); // 获取 boundary string boundary = mht.HeaderPart.ContentType.Boundary; //if (boundary[0] == '"') // boundary = boundary.Trim('"'); // 分割 //var contentParts = content.Split(new[] { boundary }, StringSplitOptions.RemoveEmptyEntries); string[] contentParts = Regex.Split(content, "^.*?" + Regex.Escape(boundary) + ".*" , RegexOptions.Compiled | RegexOptions.Multiline); contentParts = contentParts.Skip(0).ToArray(); // 解析每一部分 foreach (string strPart in contentParts) { string str = strPart.Trim(); if (string.IsNullOrWhiteSpace(str)) // 忽略开头和结尾的两部分 { continue; } MIMEPart part = MIMEPart.ParseContent(str); mht.Parts.Add(part); string location; if (part.Header.TryGetValue("Content-Location", out location)) { mht.Locations[location] = part; } } return(mht); }