C# 读取Outlook.msg文件
我认为读取Outlook.msg文件(为了获得额外的元数据,如主题、附件等)的唯一方法是使用outlookapi,即C# 读取Outlook.msg文件,c#,com,outlook,C#,Com,Outlook,我认为读取Outlook.msg文件(为了获得额外的元数据,如主题、附件等)的唯一方法是使用outlookapi,即Application.Session.OpenSharedItem()方法 如果是这种情况,那么我正在寻找在我们的应用服务器上运行此代码的方法,该应用服务器没有安装MS OFfice或MS Outlook。我发现了错误 System.ArgumentException: progId not found. Outlook.Application 这当然是由于缺少Outlook应
Application.Session.OpenSharedItem()
方法
如果是这种情况,那么我正在寻找在我们的应用服务器上运行此代码的方法,该应用服务器没有安装MS OFfice或MS Outlook。我发现了错误
System.ArgumentException: progId not found. Outlook.Application
这当然是由于缺少Outlook应用程序造成的
为了让OpenSharedItem方法正常工作,有没有办法只安装一个DLL或其他东西?如果可能的话,我不想安装完整的客户端
或者,有没有一种方法可以解析.msg文件而不需要像Outlook这样的重要依赖项?这在我很久以前保存的一篇codeplex文章中得到了回答 文章中提到,有一个名为OutlookStorage.cs的文件不需要outlook模型 如下面的评论所示,现在有一个nuget软件包涵盖了这一点: 西蒙·格林在此评论中的道具。您也可以
GetMessageFromMsgFile
方法:是用于.NET Framework的Microsoft Outlook.msg文件API。该API允许您轻松创建/读取/解析/转换.msg文件等。该API不需要在计算机或任何其他第三方应用程序或库上安装Microsoft Outlook即可工作。以下是读取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);
}
总是有办法的!我不确定复制DLL来访问API是否合法。自己解析文件至少可以避免这些复杂问题。我想我在这里找到了一个类似的实现-它是一个NuGet包:。我之所以这样做,是因为下载比注册CodeProject网站更容易。它似乎工作得很好-比谢谢你的时间是的,我也找到了这个…但是,我更喜欢MSGReader NuGet软件包和OutlookStorage CodeProject解决方案,因为它们是免费的
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);
}
Install-Package MSGReader