在 C# 中将多个 eml 文件转换为单个 PST
我需要编写一个函数,它将获取多个 eml 文件(可能来自单个文件系统文件夹)并将它们转换为单个 PST 文件。
是否可以?如果是,有人可以提供示例代码吗?
我认为这是可能的,因为有许多商业 eml 到 pst 转换器正在这样做
I need to write a single function which will take multiple eml files ( may be from a single filesystem folder ) and convert them to a single PST file.
Is it possible? if yes can someone provide a sample code?
I assume its possible because there are many commercial eml to pst converters out there doing this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
虽然 Outlook 可以打开 EML 文件,但仅使用 VBA 无法以编程方式实现。因此,我创建了这个 VBA 宏,它循环遍历某个文件夹并使用 SHELL EXEC 打开每个 EML 文件。 Outlook 可能需要几毫秒才能打开 EML 文件,因此 VBA 会等待,直到在 ActiveInspector 中打开某些内容。最后,将此电子邮件复制到某个选定的文件夹中,并且(如果成功)原始 EML 文件将被删除。
该宏有时会崩溃,但您可以随时重新启动该宏,并且它将从之前崩溃的位置重新启动(请记住,所有成功导入的 EML 文件都将被删除)。如果重启后仍然崩溃,则可能是下一个要导入的 EML 文件出现问题。在这种情况下,您只需删除有问题的 EML 即可。
PS:有时您可以自己打开 EML,而不会导致 Outlook 崩溃,但根据我的测试,每次 EML 文件导致 Outlook 崩溃时,都是一些不重要的事情,例如已读回执。
这是我的VBA代码。如果您有任何疑问或问题,请告诉我。
Although Outlook can open EML files, there is no way to do it programatically only with VBA. So I created this VBA macro which loops through some folder and opens each EML file using SHELL EXEC. It may take a few milliseconds until Outlook opens the EML file, so the VBA waits until something is open in ActiveInspector. Finally, this email is copied into some chosen folder, and (in case of success) the original EML file is deleted.
This macro crashes sometimes, but you can restart the macro at any time, and it will restart from where it previously crashed (remember, all successfully imported EML files are deleted). If it keeps crashing after restart, then probably there is a problem with the next EML file which is about to be imported. In this case you can just delete the problematic EML.
PS: Sometimes you can open the EML yourself, without crashing Outlook, but according to my tests, everytime that a EML file was crashing Outlook it was something unimportant, like read receipts.
Here follows my VBA code. If you have any doubts or problems, let me know.
很可能是更简单或更好的方法,但一种方法可能是使用 Interop 来自动化 Outlook。可能有一些使用 Outlook 内置导入功能的能力,这将是我尝试寻找的第一件事。假设这是不可能的,您仍然应该能够通过读取应用程序中的 eml 文件,然后通过 Interop 创建邮件项目来完成此操作。
通常,eml 文件只是 MIME 格式的文本文件,因此只需将它们作为文本文件读取并解析即可。 这里是一篇关于从 C# 解析 MIME 的文章,否则只需搜索“ POP3 C#”,您会找到相关的其他文章。
然后,您可以按照 此处。
据猜测,我假设您可能必须首先创建一个
Application
对象,然后使用它来获取Store
对象(我认为每个 PST 文件将是一个< code>Store),然后是其中的Folder
,然后找到某种方法使用从 eml 文件解析的数据来创建MailItem
。本文介绍如何使用 Outlook 自动化创建联系人和约会,以及可能会有用。
Might very well be easier or better ways but one way would probably be to use Interop to automate Outlook. There might be some ability to use the built in Import features of Outlook and that would be the first thing I'd try looking for. Assuming that that's not possible, you should still be able to do it by reading the eml files in your app and then creating the mail items via Interop.
Normally eml files are just text files in MIME format so that's just a matter of reading them in as text files and parsing them. Here's one article about parsing MIME from C# and otherwise just search for "POP3 C#" and you'll find other articles about that.
Then you use Outlook Interop from the namespace
Microsoft.Office.Interop.Outlook
as is described here.At a guess I'd assume that you might have to first create an
Application
object, then use that to get theStore
object (I think each PST file will be oneStore
) and then theFolder
in there and then find some way to create theMailItem
using the data you parsed from the eml file.This article describes using Outlook automation to create contacts and appointments and could probably be useful.
您可以使用 Redemption (我是它的作者)。大致思路(VBA):
You can use Redemption (I am its author) for that. Something along the lines (VBA):
您可以在此处找到 pst 文件格式的规范。但我想您会花一些时间将它们放在一起来自己创建一个 eml->pst 解析器。但这应该是可能的。
You can find the specifications to the pst file format here. But I guess you would spend some time putting it all together to create a eml->pst parser yourself. But it should be possible.