C# 从 .msg 文件中读取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-01 08:51:46  来源:igfitidea点击:

Read from .msg files

提问by huseyint

I need to read from Outlook .MSG file in .NET withoutusing COM API for Outlook (cos it will not be installed on the machines that my app will run). Are there any free 3rd party libraries to do that? I want to extract From, To, CC and BCC fields. Sent/Receive date fields would be good if they are also stored in MSG files.

我需要在 .NET 中读取 Outlook .MSG 文件,而不使用 Outlook 的 COM API(因为它不会安装在我的应用程序将运行的机器上)。有没有免费的 3rd 方库可以做到这一点?我想提取 From、To、CC 和 BCC 字段。如果发送/接收日期字段也存储在 MSG 文件中,则它们会很好。

采纳答案by huseyint

Update:I have found a 3rd party COM library called Outlook Redemptionwhich is working fine for me at the moment. If you use it via COM-Interop in .NET, don't forget to release every COM object after you are done with it, otherwise your application crashes randomly.

更新:我发现了一个名为Outlook Redemption的 3rd 方 COM 库,它目前对我来说很好用。如果您在 .NET 中通过 COM-Interop 使用它,请不要忘记在完成后释放每个 COM 对象,否则您的应用程序会随机崩溃。

回答by Jarod Elliott

If you open the .MSG file in a text editor, i believe you will find that the information you are after is stored as plain text inside the file. (It is on all the messages i have checked at least)

如果您在文本编辑器中打开 .MSG 文件,我相信您会发现您所追求的信息以纯文本形式存储在文件中。(这是我至少检查过的所有消息)

It would be pretty easy to write some code to parse the file looking for lines beginning with "From:" or "To:" etc. and then extracting the information you need.

编写一些代码来解析文件以查找以“From:”或“To:”等开头的行,然后提取您需要的信息会很容易。

If you need the body of the email as well, that may be a bit more complicated.

如果您还需要电子邮件正文,那可能会更复杂一些。

回答by Robby Slaughter

Microsoft has documented this: .MSG File Format Specification

Microsoft 已记录此内容:.MSG 文件格式规范

回答by Shog9

It's a "Structured Storage" document. I've successfully used Andrew Peace's codeto read these in the past, even under .NET (using C++/CLI) - it's clean and fairly easy to understand. Basically, you need to figure out which records you need, and query for those - it gets a little bit hairy, since different versions of Outlook and different types of messages will result in different records...

这是一个“结构化存储”文档。过去我已经成功地使用Andrew Peace 的代码来阅读这些代码,即使在 .NET 下(使用 C++/CLI) - 它很干净并且相当容易理解。基本上,您需要弄清楚您需要哪些记录,并查询这些记录 - 这有点麻烦,因为不同版本的 Outlook 和不同类型的消息会导致不同的记录......

回答by Paul Batum

There is code avaliable on CodeProject for reading .msg files without COM. See here.

CodeProject 上有代码可用于在没有 COM 的情况下读取 .msg 文件。见这里

回答by Knox

Here's some sample VBA code using Outlook Redemptionthat Huseyint found.

下面是 Huseyint 发现的一些使用Outlook Redemption 的示例 VBA 代码。

Public Sub ProcessMail()

   Dim Sess As RDOSession
   Dim myMsg As RDOMail
   Dim myString As String

   Set Sess = CreateObject("Redemption.RDOSession")
   Set myMsg = Sess.GetMessageFromMsgFile("C:\TestHarness\kmail.msg")

   myString = myMsg.Body
   myMsg.Body = Replace(myString, "8750", "XXXX")

   myMsg.Save

End Sub

回答by Martin Vobr

You can try our (commercial) Rebex Secure Maillibrary. It can read Outlooks MSG format. Following code shows how:

您可以尝试我们的(商业)Rebex Secure Mail库。它可以读取 Outlooks MSG 格式。以下代码显示了如何:

// Load message
MailMessage message = new MailMessage();
message.Load(@"c:\Temp\t\message.msg");

// show From, To and Sent date
Console.WriteLine("From: {0}", message.From);
Console.WriteLine("To: {0}", message.To);
Console.WriteLine("Sent: {0}", message.Date.LocalTime);

// find and try to parse the first 'Received' header
MailDateTime receivedDate = null;
string received = message.Headers.GetRaw("Received");
if (received != null)
{
    int lastSemicolon = received.LastIndexOf(';');
    if (lastSemicolon >= 0)
    {
        string rawDate = received.Substring(lastSemicolon + 1);
        MimeHeader header = new MimeHeader("Date", rawDate);
        receivedDate = header.Value as MailDateTime;
    }
}

// display the received date if available
if (receivedDate != null)
    Console.WriteLine("Received: {0}", receivedDate.LocalTime);

More info on Sent and Received dates and how are they represented in the message can be found at http://forum.rebex.net/questions/816/extract-senttime-receivetime-and-time-zones

有关发送和接收日期以及它们在消息中如何表示的更多信息,请访问http://forum.rebex.net/questions/816/extract-senttime-receivetime-and-time-zones