Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open .eml files using Outlook MAPI in C#?

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?

like image 819
CubaLibre Avatar asked May 17 '11 08:05

CubaLibre


People also ask

How do I open an EML file as an email attachment?

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.

How do I open an EML file on a PC?

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.

Can you open EML files without Outlook?

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.


Video Answer


2 Answers

Try this sample code Easily Retrieve Email Information from .EML Files

like image 61
lsalamon Avatar answered Sep 27 '22 20:09

lsalamon


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.)

like image 28
Dmitry Streblechenko Avatar answered Sep 27 '22 20:09

Dmitry Streblechenko