使用 Thunderbird 编程

读取消息

Mozilla Thunderbird 是由 Mozilla 基金会开发的开源跨平台电子邮件客户端。它将电子邮件存储在自己的文件结构中,通过专有文件格式管理消息索引和子文件夹。Aspose.Email 可以与 Thunderbird 的邮件存储结构协同工作。MboxrdStorageReader 类使开发者能够从 Mozilla Thunderbird 的邮件存储文件读取消息。本文介绍如何从 Thunderbird 邮件存储中读取消息:

  1. 打开 Thunderbird 的存储文件
  2. 创建 MboxrdStorageReader 类的实例,并将上述流传递给构造函数。
  3. 调用 read_next_message() 获取第一条消息。
  4. 在 while 循环中使用相同的 read_next_message() 来读取所有消息。
  5. 关闭所有流。

下面的代码片段展示了如何读取 Thunderbird 邮件存储中的所有消息。

检索消息属性

为了读取和检索 Mbox 文件中的信息,Aspose.Email 提供了 MboxStorageReader 用于为 Mbox 文件创建读取器对象的类以及 MboxLoadOptions 用于加载文件的类。以下属性属于该类的 MboxMessageInfo 类可用于访问和显示特定消息详细信息:

  • ‘date’ - 获取邮件日期。
  • ‘from_address’ - 获取发件人地址。
  • ‘subject’ - 获取消息主题。
  • ’to’ - 获取包含消息收件人的地址集合。
  • ‘cc’ - 获取包含 CC 收件人的地址集合。
  • ‘bcc’ - 获取包含消息 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}")

通过标识符从 MBOX 提取消息

为了从 MBOX 文件读取消息,Aspose.Email 提供了该类的 ‘create_reader()’ 方法。 MboxStorageReader 类,用于为文件创建读取器对象。它接受文件名和 MboxLoadOptions 作为参数,允许用户在需要时使用特定选项加载 MBOX 文件。

为了提取消息,使用了以下方法和属性:

  • ’enumerate_message_info()’ 方法属于 MboxStorageReader 类 - 遍历 MBOX 文件中的每条消息。
  • ’extract_message()’ 方法属于 MboxStorageReader 类 - 按入口 ID 提取每条消息。
  • ’entry_id’ 属性属于 MboxMessageInfo 类 - 获取入口标识符。

最后,使用以下方式将消息转换为 EML 格式: EmlLoadOptions.

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

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’ 属性在加载 EML 文件时保留 TNEF 附件。 EmlLoadOptions 类。

您可以使用指定的加载选项,通过该类的 ‘read_next_message’ 方法读取 mbox 文件中的下一封电子邮件。 MboxStorageReader 类,并使用其 ‘mbox_to_pst’ 方法将文件转换为 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 转换为 PST(保留或移除签名)

在转换过程中若要从文件中移除签名,请将 MboxToPstConversionOptions.remove_signature 属性设为 true。

以下代码示例演示如何使用此属性:

import aspose.email as ae

personalStorage = ae.storage.pst.PersonalStorage.create("target.pst", ae.storage.pst.FileFormatVersion.UNICODE)
conversion_options = ae.storage.MboxToPstConversionOptions()
conversion_options.remove_signature = True
ae.storage.MailStorageConverter.mbox_to_pst( ae.storage.mbox.MboxrdStorageReader("source.mbox", ae.storage.mbox.MboxLoadOptions()), personalStorage, "Inbox", conversion_options)

写入消息

MboxrdStorageWriter 类提供向 Thunderbird 邮件存储文件写入新消息的功能。写入消息:

  1. 在 FileStream 中打开 Thunderbird 存储文件。
  2. 创建 MboxrdStorageWriter 类的实例,并将上述流传递给构造函数。
  3. 使用 MailMessage 类准备一条新消息。
  4. 调用 write_message() 方法,并将上述 MailMessage 实例传入,以将消息添加到 Thunderbird 存储中。
  5. 关闭所有流。

下面的代码片段展示了如何将消息写入 Thunderbird 的邮件存储。

获取 MBox 文件中的消息总数

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

获取当前消息大小