之前使用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();
}


 

上面是解析附件的一段代码,正文、主题等更简单了