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?
eml file, use the "Mail" application on your device. Right-click on your . eml file and select "Open With." Then, click the stamp icon that says "Mail." You can open your . eml files in the "Mail" application even if you don't have an account.
Manually Open EML Files in WindowsOpen Windows File Explorer and locate the EML file you want to open. Right-click the EML file and select Open With. Select Mail or Windows Mail. The file opens in the Windows email program.
EML files are essentially emails in file form. The easiest way to open them is to use an email client such as Outlook, Outlook Express, Windows Live Mail, or Thunderbird. For most newer versions of these programs, EML files should be automatically registered so that double-clicking the file opens it.
Try this sample code Easily Retrieve Email Information from .EML Files
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 - I am its author):
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With