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
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.
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.
You can use Redemption for that. Something along the lines:
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.