Read Outlook .msg File

六眼飞鱼酱① 提交于 2019-12-01 03:18:56

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.

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.

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

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