之前使用lumisoft解析eml,總是會出現很奇怪的問題,所以改使用微軟自家的com庫,確實厲害兼容性更好,代碼
string file = emailPath; CDO.Message oMsg = new CDO.Message(); ADODB.Stream stm = null; //讀取EML文件到CDO.MESSAGE,做分析的話,實際是用了下面的部分 try { stm = new ADODB.Stream(); stm.Open(System.Reflection.Missing.Value, ADODB.ConnectModeEnum.adModeUnknown, ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, "", ""); stm.Type = ADODB.StreamTypeEnum.adTypeBinary;//二進制方式讀入 stm.LoadFromFile(file); //將EML讀入數據流 oMsg.DataSource.OpenObject(stm, "_stream"); //將EML數據流載入到CDO.Message,要做解析的話,后面就可以了。 CDO.IBodyParts ip = oMsg.Attachments; int count = oMsg.Attachments.Count; if (count != 0) { for (int i = 1; i <= count; i++) { ////獲取到附件的文件名稱+后綴 object FileName = oMsg.Attachments[i].FileName; //object fileContext=oMsg.Attachments[i].GetStream(); //內容 oMsg.Attachments[i].SaveToFile(@"C:\" + FileName); //ip.GetEnumerator().Current; } MessageBox.Show("下載完成,保存到:C:\\根目錄"); } else { MessageBox.Show("沒有附件"); } } catch (IOException ex) { } finally { stm.Close(); }
上面是解析附件的一段代碼,正文、主題等更簡單了