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

前端 未结 4 660
眼角桃花
眼角桃花 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:21

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

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

提交回复
热议问题