将 .MSG 文件转换为 .TXT;我应该使用 Microsoft.Interop Outlook 吗?

Converting .MSG files to .TXT; Should I use Microsoft.Interop Outlook?

我正在尝试将 .msg 文件转换为 .txt。我有两个问题。

1)我一直在调查并找到了 Microsoft.Interop Outlook 包,并且有一种方法可以提取 bodyHTML、收件人、发送日期和其他一些属性,但我觉得这是一个非常手动的过程,因为我必须 trim 输出所有 html 标签,例如 < br>、 、href 等...

这是我当前的代码...

MailItem mailItem = outlookApp.Session.OpenSharedItem(item) as MailItem;
TextFile textFile = new TextFile(); //collection of properties I am interested in
textFile.To = mailItem.To;
textFile.Subject = mailItem.Subject;
textFile.Sent = mailItem.SentOn.ToString();
textFile.Name = Path.GetFileNameWithoutExtension(item);
var atttach = mailItem.Attachments;  //Really just want the names 
textFile.Body = RemoveStuff(mailItem.HTMLBody); //manually removing all html tags
textFiles.Add(textFile);
Marshal.ReleaseComObject(mailItem);

有谁知道在 C# 中是否有更有效的方法或我不知道的使用 Interop 的方法?

2) 如果我走互操作路线,有没有办法绕过 Outlook 中询问我是否可以允许访问 Outlook 的弹出窗口?如果我的目标是创建一个转换器,那么效率似乎很低。

非常感谢任何帮助。

谢谢!

首先,您为什么使用 HTMLBody 属性 而不是纯文本 Body?

其次,您可以使用MailItem.SaveAs(..., olTxt) 将消息保存为文本文件。还是 txt 文件是其他意思?

如果您的防病毒应用不是最新的,Outlook 会发出安全提示。如果您无法控制代码运行的环境,请使用扩展 MAPI(仅限 C++ 或 Delphi)或类似 Redemption (any language - I am its author) are pretty much your only option. See http://www.outlookcode.com/article.aspx?id=52 的包装器了解更多详细信息。

Redemption中,你可以有如下内容:

using Redemption;
...
RDOSession session = new RDOSession();
RDOMail msg = session.GetMessageFromMsgFile(TheFileName);
msg.SaveAs(TxtFileName, rdoSaveAsType.olTXT);