'Importing EML (MIME) files in Outlook programmatically
I have a directory full of email files I want to import into Outlook.
These files should be, AFAIK, in RFC822 format. Opening them with Notepad++ shows the following and more plaintext
Return-Path: XXX
Received: XXX
Authentication-Results: XXX
X-Message-Status: XXX
X-SID-PRA: XXX
X-SID-Result: XXX
X-AUTH-Result: XXX
X-Message-Delivery: XXX
X-Message-Info: XXXX
Received: from XXX
Received: from XXX
DomainKey-Signature: XXX
So I guess they are in plain old format. Now I would like to import them into a PST file in order to index and make them searcheable. I have very poor knowledge of Outlook Interop.
Renaming the untitled files to .eml works, I can open them with Outlook 2010. Dragging them into Outlook windows half works: emails are not previewed but I can double-click them. Not all emails get imported (I'm talking about 3000+ messages), and Outlook doesn't ingore damaged files, so either all or none.
I was exploring Outlook interop.
The basic idea is the following: for each text file, import that as email into a given folder.
I have tried to write, but not yet to test, the following code
Application outlook = GetApplicationObject();
outlook.Session.AddStore(pstPath);
Store theStore = outlook.Session.Stores.Cast<Store>().FirstOrDefault(store => store.FilePath == pstPath);
if (theStore == null) throw new Exception();
try
{
string[] files = Directory.GetFiles(dataDirectory, "*", SearchOption.AllDirectories);
Folder folder = (Folder)theStore.GetRootFolder();
foreach (string file in files.Where(file => file.EndsWith(".eml")))
{
//What?
}
}
finally
{
//Dispose??
}
Where GetApplicationObject is copied from another SO answer.
Question: is it possible to create a MailItem based on the contents of a file? Or do I have to use an email reader and then create the MailItem field by field? Like the following I was writing...
IMail mail = builder.CreateFromEmlFile(file);
MailItem outlookMail = outlook.CreateItem(OlItemType.olMailItem);
outlookMail.Body = mail.GetBodyAsText();
outlookMail.CC = string.Join("; ", mail.Cc.Select(x => x.Render()));
outlookMail.HTMLBody = mail.GetBodyAsHtml();
if (MimeImportance.High.Equals(mail.Importance))
outlookMail.Importance = OlImportance.olImportanceHigh;
else if (MimeImportance.Low.Equals(mail.Importance))
outlookMail.Importance = OlImportance.olImportanceLow;
else
outlookMail.Importance = OlImportance.olImportanceNormal;
outlookMail.Sender=outlook.CreateItem()
Library I'm trying is from http://www.limilabs.com/mail
Solution 1:[1]
If you were using C++ or Delphi, you could use the IConverterSession MAPI interface - you can play with it in OutlookSpy (I am its author) if you click the IConverterSession button.
Otherwise you can use Redemption (I am also its author) and its RDOMail object - its Import method supports MIME format (among a dozen or so other formats).
set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
set Inbox = Session.GetDefaultFolder(olFolderInbox)
set Msg = Inbox.Items.Add
Msg.Sent = true
Msg.Import "C:\Temp\test.eml", 1024
Msg.Save
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
