How to import MSG/EML email files into Outlook via the API?

浪子不回头ぞ 提交于 2021-01-29 03:50:27

问题


I have a directory hierarchy full of EML/MSG email data files. These files can be imported into MS Outlook 2010 (desktop version) one at a time, but I am looking for a way to automate this. I need to create each folder as an Outlook folder, and import email datafiles into these Outlook folders. Anyone aware of an Outlook 2010 API that allows for creating folders & importing files?


回答1:


To create new folders in Outlook, use MAPIFolder.Folders.Add.

Outlook Object Model does not directly support importing EML or MSG files (or any other files for that matter).

For MSG files you can

  1. Use Extended MAPI (C++ or Delphi oinly) and the OpenIMsgOnIStg function to open an MSG file. You can then create a new message in the target folder and copy properties either using IMesage.CopyTo (keep in mind that standalone MSG files do not correctly process named properties in CopyTo) or read the properties one at a time and set them on the target message explicitly.

  2. Outlook Object Model can open MSG files using Application.CreateItemFromTemplate (it will be created in an unsent state) and using Namespace.OpenSharedItem. You can then move the message (MailItem.Move) into the target folder

For the EML files, you can

  1. Use Extended MAPI (C++ or Delphi only) and the built-in Outlook interface (IComverterSession). You can play with that interface in OutlookSpy (click IConverterSession button).

  2. Outlook Object Model does not support EML files at all. The best you can do is create your own parser and copy the EML file into Outlook one MIME header/part at a time.

If using Redemption is an option, you can use RDOMail.Import method - it imports MSG (olMsg) and EML (olRfc822) files (as well as a few other formats).

  set Session = CreateObject("Redemption.RDOSession")
  Session.MAPIOBJECT = Application.Session.MAPIOBJECT '//or you can call Logon
  set Inbox = Session.GetDefaultFolder(olFolderInbox)
  set Msg = Inbox.Items.Add
  Msg.Sent = true '//since Import does not copy this property
  Msg.Import("c:\temp\test.eml", 1024) ' //1024 is olRfc822
  Msg.Save


来源:https://stackoverflow.com/questions/25602282/how-to-import-msg-eml-email-files-into-outlook-via-the-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!