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

时间:2017-10-07 17:04:54

标签: c# text outlook interop msg

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

1)我一直在调查并找到了Microsoft.Interop Outlook软件包,我可以通过这种方式提取bodyHTML,To,Sent Date和其他一些属性,但我觉得这是一个非常手动的过程,因为我必须修剪所有的html标签,如< br>,& nbsp,a 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?如果我的目标是创建转换器,那似乎效率低下。

非常感谢任何帮助。

谢谢!

1 个答案:

答案 0 :(得分:1)

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

其次,您可以使用MailItem.saveAs(...,olTxt)将消息另存为文本文件。或者你的意思是txt文件的其他东西吗?

如果您的防病毒应用程序不是最新的,则Outlook会引发安全提示。如果您无法控制运行代码的环境,则扩展MAPi(仅限C ++或Delphi)或类似Redemption(任何语言)的包装器几乎是您唯一的选择。有关详细信息,请参阅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);