从 Mozilla Thunderbird 读取消息

Mozilla Thunderbird 是由 Mozilla 基金会开发的流行开源跨平台电子邮件客户端。它将电子邮件存储在自己的文件结构中,并使用专有文件格式管理消息索引和子文件夹。Aspose.Email 能够与 Thunderbird 邮件存储结构协同工作。具体而言,API 提供了 MboxrdStorageReader 类,允许开发者读取 Mozilla Thunderbird 邮件存储文件中的消息。本文说明了如何使用 Aspose.Email 读取 Thunderbird 邮件存储中的消息。

如何读取 MBOX 文件中的消息

要从 Thunderbird 邮件存储读取消息,请按以下步骤操作:

  1. 打开 Thunderbird 存储文件。
  2. 创建该类的实例 MboxrdStorageReader 类并将文件流传递给其构造函数。
  3. 调用 read_next_message() 检索第一条消息的方法。
  4. 使用带有 read_next_message() 的 while 循环遍历所有消息。
  5. 处理完毕后关闭所有文件流。

以下代码片段演示了如何读取 Thunderbird 邮件存储文件中的所有消息:

从 MBOX 消息检索电子邮件属性

Aspose.Email 提供了 MboxStorageReader 用于读取消息的类以及 MboxLoadOptions 用于加载 MBOX 文件的类。该 MboxMessageInfo 类包含各种属性,以访问和显示特定的消息详情,包括:

  • date:检索邮件日期。

  • from_address:检索发件人地址。

  • subject:检索邮件主题。

  • to:检索收件人地址。

  • cc:检索抄送收件人地址。

  • bcc:检索密件抄送收件人地址。

以下代码片段演示了如何从 MBOX 文件中提取并显示消息详情:

import aspose.email as ae

reader = ae.storage.mbox.MboxStorageReader.create_reader(file_name, ae.storage.mbox.MboxLoadOptions())

for mbox_message_info in reader.enumerate_message_info():
    print(f"Subject: {mbox_message_info.subject}")
    print(f"Date: {mbox_message_info.date}")
    print(f"From: {mbox_message_info.from_address}")
    print(f"To: {mbox_message_info.to}")
    print(f"CC: {mbox_message_info.cc}")
    print(f"Bcc: {mbox_message_info.bcc}")

通过 ID 从 MBOX 文件中提取消息

Aspose.Email 允许使用 Entry ID 从 MBOX 文件中提取消息。以下方法和属性有助于此过程:

下面的代码示例展示了使用这些功能从 MBOX 文件读取并提取消息的方法:

  1. 使用 MboxStorageReader.create_reader() 方法。指定要处理的文件并传递 MboxLoadOptions 以配置加载选项。
  2. 通过在读取器上调用 enumerate_message_info() 来遍历每个 mbox_message_info,枚举消息。这可访问 MBox 文件中每封电子邮件的元数据。
  3. 提取单个消息。对于每个消息信息条目,使用 extract_message() 方法提取实际的电子邮件。从 mbox_message_info 中传递 entry_idEmlLoadOptions() 用于电子邮件的加载配置。
import aspose.email as ae

reader = ae.storage.mbox.MboxStorageReader.create_reader("my.mbox", ae.storage.mbox.MboxLoadOptions())

for mbox_message_info in reader.enumerate_message_info():
    eml = reader.extract_message(mbox_message_info.entry_id, ae.EmlLoadOptions())

读取 MBOX 文件时配置加载选项

Aspose.Email EmlLoadOptions 类允许您自定义从 EML 格式加载 MailMessage 的过程。例如,您可以通过设置 preserve_tnef_attachments 属性的 EmlLoadOptions 类。

要使用指定的加载选项读取 MBOX 文件中的下一封电子邮件,请使用 read_next_message 方法的 MboxStorageReader 类。此外,您可以使用 mbox_to_pst 方法的 MailStorageConverter 类。

以下代码示例演示如何使用这些方法和属性来管理电子邮件存储文件,包括从 MBOX 格式读取消息、保留 TNEF 附件以及将消息从 MBOX 转换为 PST 格式:

import aspose.email as ae

reader = ae.storage.mbox.MboxrdStorageReader(fileName, ae.storage.mbox.MboxLoadOptions())

# Read messages preserving tnef attachments.
load_options = ae.EmlLoadOptions()
load_options.preserve_tnef_attachments = True
eml = reader.read_next_message(load_options)
ae.storage.MailStorageConverter.MboxMessageOptions(load_options)

# Convert messages from mbox to pst preserving tnef attachments.
pst = ae.storage.MailStorageConverter.mbox_to_pst("Input.mbox", "Output.pst")

设置读取 MBOX 文件的首选文本编码

您可以指定加载 MBOX 文件时使用的文本编码。该 preferred_text_encoding 属性的 MboxLoadOptions 该类设置了额外选项,确保对包含编码内容的消息能够正确读取和处理。

以下代码片段展示了在项目中使用此功能的方法:

import aspose.email as ae

load_options = ae.storage.mbox.MboxLoadOptions()
load_options.preferred_text_encoding = 'utf-8'
reader = ae.storage.mbox.MboxrdStorageReader("sample.mbox", load_options)
message = reader.read_next_message()

获取 MBOX 文件中的消息总数

MboxrdStorageReader 类提供了读取 MBox 文件中可用项数量的功能。这可用于开发在处理此类文件时显示进度的应用程序。

获取 MBOX 中特定消息的大小