14

I believe that the only way to read an Outlook .msg file (in order to extra metadata like subject, attachments etc), is to use the Outlook API - the Application.Session.OpenSharedItem() method.

If this is the case, then I am looking for way to run this code on our application server, which doesn't have MS OFfice or MS Outlook installed. I'm getting the error

System.ArgumentException: progId not found. Outlook.Application

Which of course is due to the absence of the Outlook application.

Is there any way to install just a DLL or something in order to get the OpenSharedItem method to work? I don't want to have to install the full client if possible.

Or, is there a way to parse a .msg file without requiring significant dependencies such as Outlook?

1

4 Answers 4

18

This was answered in a codeplex article I have saved from a long time ago

Article is here, there is a file called OutlookStorage.cs that does not require the outlook model.

As in the comments below there is now a nuget package that covers this:

here

Props to Simon Green in the comments for this.

1
  • 8
    I think I found a similar implementation here - it's a NuGet package:https://www.nuget.org/packages/MSGReader/. I went for this because it was easier to download than signing up to the CodeProject site. It seems to work fine - thanks for your time though Oct 29, 2014 at 20:07
1

MSG .NET is Microsoft Outlook .msg file API for .NET Framework. The API allows you to easy create/read/parse/convert .msg files and more. The API does not require Microsoft Outlook to be installed on the machine or any other third party application or library in order to work.

1
  • 2
    Yes, I did also find this one... however, I prefer the MSGReader NuGet package and the OutlookStorage CodeProject solutions because they are free Oct 30, 2014 at 11:19
1

Here is the solution to read the attachement in an msg file

 try
                {
                    if (fileInfo.Extension.ToLower().Equals(".msg"))
                    {
                        string referenceNumber = "";
                        if (char.IsDigit(fileInfo.Name.First()))
                        {
                            referenceNumber = new string(fileInfo.Name.SkipWhile(c => !char.IsDigit(c)).TakeWhile(char.IsDigit).ToArray());
                        }
                        using (var stream = File.Open(fileInfo.FullName, FileMode.Open, FileAccess.Read))
                        {
                            using (var message = new Storage.Message(stream))
                            {
                                foreach (Storage.Attachment attachment in message.Attachments.OfType<Storage.Attachment>())
                                {

                                    if (attachment.IsInline)
                                        continue; //no need to uncompress inline attqach


                                    string opFilename = (referenceNumber.Trim().Length > 0) ? string.Format("{0}\\{1}_{2}", fileInfo.Directory.FullName, referenceNumber, attachment.FileName) : string.Format("{0}\\{1}_{2}", fileInfo.Directory.FullName, RandomString(3), attachment.FileName);
                                    File.WriteAllBytes(opFilename, attachment.Data);

                                }
                            }
                        }

                    }
                }
                catch (Exception ex)
                {
                    _log.ErrorFormat("{0} Unable to convert  msg file: {1}.", fileInfo.Name, ex.Message);
                }

following library is used

using MsgReader.Outlook; to install the above dll, go to nuget package manger and run

Install-Package MSGReader
1

You can also

  1. explicitly parse the MSG file (its format is documented).

  2. Use Extended MAPI (C++ or Delphi only) along with the standalone version of MAPI. See OpenIMsgOnIStg function on MSDN.

  3. Use Redemption (I am its author), requires a MAPI system - Outlook or the standalone version of MAPI must be installed - and its RDOSession.GetMessageFromMsgFile method:

set Session = CreateObject("Redemption.RDOSession")
set Msg = Session.GetMessageFromMsgFile("c:\temp\temp.msg")
MsgBox Msg.Body

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.