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?

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật