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

前端 未结 4 672
眼角桃花
眼角桃花 2021-01-12 19:48

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 l

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-12 20:07

    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:\TestEML\EmlMail.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
    

提交回复
热议问题