Recommendations on parsing .eml files in C#

前端 未结 8 1068
旧巷少年郎
旧巷少年郎 2020-11-28 05:32

I have a directory of .eml files that contain email conversations. Is there a recommended approach in C# of parsing files of this type?

8条回答
  •  一整个雨季
    2020-11-28 05:48

    I posted a sample project to illustrate this answer to Github

    The CDO COM DLL is part of Windows/IIS and can be referenced in .net. It will provide accurate parsing and a nice object model. Use it in conjuction with a reference to ADODB.DLL.

    public CDO.Message LoadEmlFromFile(String emlFileName)
    {
        CDO.Message msg = new CDO.MessageClass();
        ADODB.Stream stream = new ADODB.StreamClass();
    
        stream.Open(Type.Missing, ADODB.ConnectModeEnum.adModeUnknown, ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, String.Empty, String.Empty);
        stream.LoadFromFile(emlFileName);
        stream.Flush();
        msg.DataSource.OpenObject(stream, "_Stream");
        msg.DataSource.Save();
    
        stream.Close();
        return msg;
    }
    

    -- Added August 2017: Also check out MimeKit: https://github.com/jstedfast/MimeKit. It supports .Netstandard, so will run cross-platform.

提交回复
热议问题