How to open .eml files using Outlook MAPI in C#?
Solution 1
Try this sample code Easily Retrieve Email Information from .EML Files
Solution 2
CreateItemFromTemplate
only works with the MSG/OFT files.
Fot the EML files you will either need to parse the file explicitly in your code or use a third party library (such as Redemption):
The following code will create an MSG file and import an EML file into it using Redemption (RDOSession object):
set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = outlookApp.Session.MAPIOBJECT
set Msg = Session.CreateMessageFromMsgFile("C:\Temp\temp.msg")
Msg.Import "C:\Temp\test.eml", 1024
Msg.Save
MsgBox Msg.Subject
You can then use the message (RDOMail) to access it various properties (Subject, Body, etc.)
CubaLibre
Updated on June 16, 2022Comments
-
CubaLibre almost 2 years
I have a C# application that reads .msg files and extracts the body and the attachments. But when I try to load a .eml file the application crashes. I am loading the files like this:
MailItem mailItem = (MailItem)outlookApp.CreateItemFromTemplate(msgFileName); mailItem.SaveAs(fullFilename, OlSaveAsType.olHTML); // save body in html format for(int i = 0; i < mailItem.Attachments.Count; i++) mailItem.Attachments[i].SaveAsFile(filename); // save attachments
This works fine with .msg files, but it doesn't work for .eml files. I don't understand why .eml files don't work, because I can open .eml files in Outlook 2010.
How can I load .eml files using the Outlook Primary Interop Assembly?
-
CubaLibre almost 13 yearsIs it possible to import an eml without redemption?
-
Dan Gøran Lunde over 11 yearsRevised version is available: Easily Retrieve Email Information from .EML Files -- Revised codeproject.com/Articles/76607/…
-
Dmitry Streblechenko over 10 yearsSure, if you parse the EML file and set various MailItem object properties one at a time.
-
MGOwen about 9 years@danglund The revised version looked more complex, was 3 largish .cs files instead of one (some containing irrelevant stuff), and wouldn't even compile for me.
-
Dmitry Streblechenko over 8 yearsThis would cause the EML file to be displayed, which might not be what the OP wants.
-
bobzer over 7 yearsHi i would like to use this code but i don't have a .msg. i need a fake .msg to import an eml ? How can i import/open an eml directly ?
-
Dmitry Streblechenko over 7 yearsThe script above creates a temporary MSG file (you can delete it later or call Msg.Delete when you are done) so that it will have something to import an EML file into. MAPI or Outlook Object Model do not natively work with EML files - you need to have either a message in one of Outlook folders, or an MSG file.