阅读Outlook .msg文件

时间:2014-10-29 14:34:28

标签: c# com outlook

我认为阅读Outlook .msg文件(为了获取主题,附件等额外元数据)的唯一方法是使用Outlook API - Application.Session.OpenSharedItem()方法。

如果是这种情况,那么我正在寻找在我们的应用服务器上运行此代码的方法,该服务器没有安装MS OFfice或MS Outlook。我收到了错误

System.ArgumentException: progId not found. Outlook.Application

当然这是由于缺少Outlook应用程序。

有没有办法只安装一个DLL或什么东西,以使OpenSharedItem方法工作?如果可能的话,我不想安装完整的客户端。

或者,有没有办法解析.msg文件而不需要像Outlook这样的重要依赖项?

4 个答案:

答案 0 :(得分:10)

我在很久以前保存过的一篇codeplex文章中回答了这个问题

文章是here,有一个名为OutlookStorage.cs的文件,不需要outlook模型。

如下面的评论中,现在有一个nuget包,涵盖了这个:

here

在评论中向Simon Green道具。

答案 1 :(得分:1)

MSG .NET是适用于.NET Framework的Microsoft Outlook .msg文件API。 API允许您轻松创建/读取/解析/转换.msg文件等。 API不需要在计算机或任何其他第三方应用程序或库上安装Microsoft Outlook以便工作。

答案 2 :(得分:0)

你也可以

  1. 显式解析MSG文件(格式为documented)。

  2. 使用扩展MAPI(仅限C ++或Delphi)和standalone version of MAPI。在MSDN上查找OpenIMsgOnIStg功能。

  3. 使用Redemption(需要MAPI系统 - 必须安装Outlook或standalone version of MAPI - 及其RDOSessionGetMessageFromMsgFile方法:

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

答案 3 :(得分:0)

这是在msg文件中读取附件的解决方案

 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);
                }

使用了以下库

  

使用MsgReader.Outlook;   要安装上述dll,请转到nuget软件包管理器并运行

Install-Package MSGReader