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?
2
Try this sample code Easily Retrieve Email Information from .EML Files
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 – 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:Temptemp.msg")
Msg.Import "C:Temptest.eml", 1024
Msg.Save
MsgBox Msg.Subject
You can then use the message (RDOMail) to access it various properties (Subject, Body, etc.)
4
In order to create a MailItem from a .eml file you can use the following two steps: at first you open an outlook process instance and then you create the MailItem with the Outlook API.
string file = @"C:TestEMLEmlMail.eml";
System.Diagnostics.Process.Start(file);
Outlook.Application POfficeApp = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application"); // note that it returns an exception if Outlook is not running
Outlook.MailItem POfficeItem = (Outlook.MailItem)POfficeApp.ActiveInspector().CurrentItem; // now pOfficeItem is the COM object that represents your .eml file
1
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.
See my complete answer (and code) here:
/a/33761441/3606250
Assuming outlook is installed…
for only a msg file, when outlook is running/already open, you can use OpenSharedItem
Microsoft.Office.Interop.Outlook.Application appOutlook = new Microsoft.Office.Interop.Outlook.Application();
var myMailItem = appOutlook.Session.OpenSharedItem(strPathToSavedEmailFile) as Microsoft.Office.Interop.Outlook.MailItem;
for eml file or msg file (Outlook will open and pop up):
var strPathToSavedEmailFile=@"C:tempmail.eml";
//Microsoft.Win32 namespace to get path from registry
var strPathToOutlook=Registry.LocalMachine.OpenSubKey(@"SoftwareMicrosoftWindowsCurrentVersionApp Pathsoutlook.exe").GetValue("").ToString();
//var strPathToOutlook=@"C:Program FilesMicrosoft OfficerootOffice16OUTLOOK.EXE";
string strOutlookArgs;
if(Path.GetExtension(strPathToSavedEmailFile)==".eml")
{
strOutlookArgs = @"/eml "+strPathToSavedEmailFile; // eml is an undocumented outlook switch to open .eml files
}else
{
strOutlookArgs = @"/f "+strPathToSavedEmailFile;
}
Process p = new System.Diagnostics.Process();
p.StartInfo = new ProcessStartInfo()
{
CreateNoWindow = false,
FileName = strPathToOutlook,
Arguments = strOutlookArgs
};
p.Start();
//Wait for Outlook to open the file
Task.Delay(TimeSpan.FromSeconds(5)).GetAwaiter().GetResult();
Microsoft.Office.Interop.Outlook.Application appOutlook = new Microsoft.Office.Interop.Outlook.Application();
//Microsoft.Office.Interop.Outlook.MailItem myMailItem = (Microsoft.Office.Interop.Outlook.MailItem)appOutlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
var myMailItem = (Microsoft.Office.Interop.Outlook.MailItem)appOutlook.ActiveInspector().CurrentItem;
//Get the Email Address of the Sender from the Mailitem
string strSenderEmail=string.Empty;
if(myMailItem.SenderEmailType == "EX"){
strSenderEmail=myMailItem.Sender.GetExchangeUser().PrimarySmtpAddress;
}else{
strSenderEmail=myMailItem.SenderEmailAddress;
}
//Get the Email Addresses of the To, CC, and BCC recipients from the Mailitem
var strToAddresses = string.Empty;
var strCcAddresses= string.Empty;
var strBccAddresses = string.Empty;
foreach(Microsoft.Office.Interop.Outlook.Recipient recip in myMailItem.Recipients)
{
const string PR_SMTP_ADDRESS = @"http://schemas.microsoft.com/mapi/proptag/0x39FE001E";
Microsoft.Office.Interop.Outlook.PropertyAccessor pa = recip.PropertyAccessor;
string eAddress = pa.GetProperty(PR_SMTP_ADDRESS).ToString();
if(recip.Type ==1)
{
if(strToAddresses == string.Empty)
{
strToAddresses = eAddress;
}else
{
strToAddresses = strToAddresses +","+eAddress;
}
};
if(recip.Type ==2)
{
if(strCcAddresses == string.Empty)
{
strCcAddresses = eAddress;
}else
{
strCcAddresses = strCcAddresses +","+eAddress;
}
};
if(recip.Type ==3)
{
if(strBccAddresses == string.Empty)
{
strBccAddresses = eAddress;
}else
{
strBccAddresses = strBccAddresses +","+eAddress;
}
};
}
Console.WriteLine(strToAddresses);
Console.WriteLine(strCcAddresses);
Console.WriteLine(strBccAddresses);
foreach(Microsoft.Office.Interop.Outlook.Attachment mailAttachment in myMailItem.Attachments){
Console.WriteLine(mailAttachment.FileName);
}
Console.WriteLine(myMailItem.Subject);
Console.WriteLine(myMailItem.Body);