Read Outlook .msg File

2020-03-12 04:23发布

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?

标签: c# com outlook
4条回答
地球回转人心会变
2楼-- · 2020-03-12 04:24

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. Look up OpenIMsgOnIStg function on MSDN.

  3. Use Redemption (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
查看更多
淡お忘
3楼-- · 2020-03-12 04:38

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.

查看更多
来,给爷笑一个
4楼-- · 2020-03-12 04:43

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
查看更多
相关推荐>>
5楼-- · 2020-03-12 04:46

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.

查看更多
登录 后发表回答