Search code examples
c#.netemailoutlookadd-in

Save Mail in MIME format (*.eml) in Outlook Add-In


I want to write a little Outlook addin (C#), which saves a selected mail (MailItem) to disk in plain MIME format (.eml). The MailItem.SaveAs() method only allows to save in .msg format. Is there any other (simple) way, to save the mail in eml-format? I want to keep all details of the original mail.

I've read something about the Outlook WebServices. Maybe I can search the exchange server for the currently in outlook selected mail and receive it again from exchange and save it as .eml? What will I need for this option?

Is it possible to convert a saved .msg to .eml in an easy way (with keeping all details, headers and so on)?

I hope someone can help me with this problem, because I have spent a couple of hours on searching for a solution without any result.


Solution

  • You can either

    1. Create MIME file explicitly in your code one property at a time. You can also use existing MIME converters (I used Lumisoft in the past) - but they won't convert Outlook messages in a single call; you will need to expliiclty build all the headers and MIME parts.

    2. Use IConverterSession object (C++ or Delphi only) - this is the same MIME converter used by Outlook. You can play with it in OutlookSpy (I am its author) - click IConverterSession button. Note that as of Outlook 2016, IConverterSession interface can be used only if running inside the outlook.exe address space (COM addin). You cannot create an instance of that interface when running in a separate process.

    3. Use Redemption (I am also its author) and its RDOMail.SaveAs or SafeMailItem.SaveAs methods - it can save in the MIME format (olRfc822) along with a dozen or so other formats. It uses IConverterSession object when it is available (Outlook 2003 and up) or its own converter otherwise. The following script (VBS) will save the currently selected message in Outlook as an EML file

        set Session = CreateObject("Redemption.RDOSession")
        Session.MAPIOBJECT = Application.Session.MAPIOBJECT
        set rItem = Session.GetMessageFromID(Application.ActiveExplorer.Selection(1).EntryID)
        rItem.SaveAs "c:\temp\test.eml", 1024`