从 .msg 文件读取

发布于 2024-07-04 21:28:44 字数 1556 浏览 7 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

杀お生予夺 2024-07-11 21:28:44

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

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

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

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)

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.

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

太阳哥哥 2024-07-11 21:28:44

您可以尝试我们的(商业)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);

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

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

// 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

难如初 2024-07-11 21:28:44

Microsoft 已对此进行了记录:.MSG 文件格式规范

Microsoft has documented this: .MSG File Format Specification

听,心雨的声音 2024-07-11 21:28:44

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

It's a "Structured Storage" document. I've successfully used Andrew Peace's code to 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...

方觉久 2024-07-11 21:28:44

以下是 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

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

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
街角迷惘 2024-07-11 21:28:44

CodeProject 上提供了无需 COM 即可读取 .msg 文件的代码。 请参阅此处

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

绳情 2024-07-11 21:28:44

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

Update: I have found a 3rd party COM library called Outlook Redemption which 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文