使用 Thunderbird 编程

从 MBOX 读取消息

Mozilla Thunderbird 是一个由 Mozilla 基金会开发的开源跨平台电子邮件客户端。它使用自己的文件结构存储电子邮件,并通过专有文件格式管理消息索引和子文件夹。Aspose.Email 能够与 Thunderbird 邮件存储结构一起工作。该 MboxrdStorageReader 类让开发者能够读取 Mozilla Thunderbird 的邮件存储文件中的消息。本文展示了如何读取 Thunderbird 邮件存储中的消息:

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

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

//Getting Marker information while reading messages from Mbox storage file
try (FileInputStream stream = new FileInputStream(dataDir + "Outlook.pst")) {
    MboxLoadOptions lo = new MboxLoadOptions();
    lo.setLeaveOpen(false);
    try (MboxrdStorageReader reader = new MboxrdStorageReader(stream, lo)) {
        MailMessage msg;
        String[] fromMarker = {null};
        while ((msg = reader.readNextMessage(/* out */fromMarker)) != null) {
            System.out.println(fromMarker[0]);
        }
    }
}

读取 MBOX 消息时配置加载选项

Aspose.Email API 在从 MBOX 文件读取消息时允许以下操作:

将 MBOX 消息转换为 PST,保留 TNEF 附件

EmlLoadOptions emlLoadOptions = new EmlLoadOptions();
emlLoadOptions.setPreserveTnefAttachments(true);
MailStorageConverter.setMboxMessageOptions(emlLoadOptions);
// Convert messages from mbox to pst preserving tnef attachments.
PersonalStorage storage = MailStorageConverter.mboxToPst("Input.mbox", "Output.pst");

MailStorageConverter.MboxMessageOptions() 属性 - 获取或设置解析 Mbox 存储时的邮件加载选项。

读取保留 TNEF 附件的消息

MboxrdStorageReader reader = new MboxrdStorageReader("Input.mbox", new MboxLoadOptions());
// Read messages preserving tnef attachments.
EmlLoadOptions emlLoadOptions = new EmlLoadOptions();
emlLoadOptions.setPreserveTnefAttachments(true);
MailMessage eml = reader.readNextMessage(emlLoadOptions);

MboxrdStorageReader.readNextMessage(EmlLoadOptions options) method - EmlLoadOptions 参数在从 Mbox 存储读取消息时指定选项。

枚举保留 TNEF 附件的消息

EmlLoadOptions emlLoadOptions = new EmlLoadOptions();
emlLoadOptions.setPreserveTnefAttachments(true);
// Enumerate messages preserving tnef attachments.
for (MailMessage message : reader.enumerateMessages(emlLoadOptions)) {
    // do something
}

MboxrdStorageReader.enumerateMessages(EmlLoadOptions options) method - 在从 Mbox 存储读取消息时指定 EmlLoadOptions。

通过标识符从 MBOX 提取消息

有时需要通过标识符提取选定的消息。例如,您的应用程序在数据库中存储标识符,并按需提取消息。这是一种高效的方式,避免每次遍历整个存储以查找特定要提取的消息。为在 MBOX 文件中实现此功能,Aspose.Email 提供以下方法和类:

下面的代码示例演示了如何通过标识符从 MBOX 提取消息:

MboxStorageReader reader = MboxStorageReader.createReader("my.mbox", new MboxLoadOptions());

for (MboxMessageInfo msgInfo : reader.enumerateMessageInfo()) {
    MailMessage eml = reader.extractMessage(msgInfo.getEntryId(), new EmlLoadOptions());
}

注意: 消息 ID 在存储文件中是唯一的。ID 由 Aspose.Email 创建,无法在其他第三方 MBOX 处理库或应用中使用。

在 MBOX 文件中筛选和搜索邮件

Aspose.Email for Java 提供使用查询在 MBOX 文件中筛选或搜索消息的能力。这使得仅检索符合特定条件的消息成为可能,从而在处理大型 MBOX 文件时提升应用程序的性能和可用性。

下面的代码示例演示了通过实现以下方法来实现此功能:

  • enumerateMessages(MailQuery query) - 返回与指定查询匹配的 MailMessage 实例的可枚举集合。

  • enumerateMessageInfo(MailQuery query) - 返回与指定查询匹配的 MboxMessageInfo 实例的可枚举集合。

MboxStorageReader reader = MboxStorageReader.createReader("input.mbox", new MboxLoadOptions());
MailQueryBuilder mqb = new MailQueryBuilder();
mqb.getSubject().contains("Project Update");
mqb.getSentDate().before(new Date());

for (MailMessage message : reader.enumerateMessages(mqb.getQuery())) {
    System.out.println("Subject: " + message.getSubject());
}

从 MBOX 文件分页检索消息

Aspose.Email for Java 支持从 MBOX 文件分页检索消息。该功能通过将消息分批检索,以较小批次处理大型 MBOX 文件,从而降低内存消耗并提升性能。

下面的代码示例演示了通过实现以下方法来实现此功能:

  • enumerateMessages(int startIndex, int count) - 从给定索引开始检索指定数量的 MailMessage 实例。

  • enumerateMessageInfo(int startIndex, int count) - 从给定索引开始检索指定数量的 MboxMessageInfo 实例。

MboxStorageReader reader = MboxStorageReader.createReader("input.mbox", new MboxLoadOptions());
int startIndex = 0;
int count = 10; // Retrieve messages in batches of 10

for (MailMessage message : reader.enumerateMessages(startIndex, count)) {
    System.out.println("Subject: " + message.getSubject());
}

写入消息

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

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

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

//Getting marker information while writing messages to Mbox storage file
try (FileOutputStream writeStream = new FileOutputStream(dataDir + "inbox")) {
    try (MboxrdStorageWriter writer = new MboxrdStorageWriter(writeStream, false)) {
        MailMessage msg = MailMessage.load(dataDir + "Message.msg");
        String[] fromMarker = {null};
        writer.writeMessage(msg, fromMarker);
        System.out.println(fromMarker[0]);
    }
}

获取 MBox 文件中的消息总数

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

MboxLoadOptions lo = new MboxLoadOptions();
try (MboxrdStorageReader reader = new MboxrdStorageReader("inbox.dat", lo)) {
    System.out.println("Total number of messages in Mbox file: " + reader.getTotalItemsCount());
}

获取当前消息大小

FileInputStream stream = new FileInputStream(dataDir + "ExampleMbox.mbox");
MboxLoadOptions lo = new MboxLoadOptions();
try (MboxrdStorageReader reader = new MboxrdStorageReader(stream, lo)) {
    MailMessage msg = null;
    while ((msg = reader.readNextMessage()) != null) {
        //returns the number of bytes read
        long currentDataSize = reader.getCurrentDataSize();
        System.out.println("Bytes read: " + currentDataSize);
    }
}

加载 Mbox 文件时设置首选文本编码

setPreferredTextEncoding(Charset value) 方法的 MboxLoadOptions 类用于获取或设置消息的首选编码。使用指定的加载选项创建 Mbox 文件的读取器,您的消息(带编码的内容)将被正确读取和处理。下面的代码示例展示了如何在项目中实现此功能:

MboxLoadOptions lo = new MboxLoadOptions();
lo.setPreferredTextEncoding(Charset.forName("utf-8"));
MboxrdStorageReader reader = new MboxrdStorageReader("sample.mbox", lo);
MailMessage message = reader.readNextMessage();

将 Mbox 存储拆分为更小的部分

Aspose.Email 提供以下组件,旨在更好地控制 Mbox 存储处理,允许您将大文件拆分为可管理的部分并在过程中实现自定义操作:

MboxStorageReader.SplitInto(long chunkSize, String outputPath, String partFileNamePrefix) 该方法是前一种方法的变体,还允许您为拆分后的 Mbox 文件名指定自定义前缀。

MboxStorageReader.setEmlCopyingEventHandler 事件 当电子邮件复制到新的 Mbox 文件之前会触发此事件。您可以在邮件处理之前自定义操作。

MboxStorageReader.setEmlCopiedEventHandler 事件 当电子邮件被复制到新的 Mbox 文件后会触发此事件。您可以对电子邮件执行后处理操作。

MboxStorageReader.setMboxFileCreatedEventHandler 事件 当创建新的 Mbox 文件时会触发此事件。您可以处理此事件以对文件创建作出响应。

MboxStorageReader.setMboxFileFilledEventHandler 事件:当新 Mbox 文件被填充电子邮件时触发此事件。您可以对文件被填充电子邮件做出响应。

NewStorageEventHandler(Object sender, NewStorageEventArgs e) 表示在创建或处理新存储文件后触发的事件的委托。

MimeItemCopyEventHandler(Object sender, MimeItemCopyEventArgs e) 表示用于处理 Mime 项目复制相关事件的委托,通常用于将 MailMessage 对象从一个存储复制到另一个存储的场景。

NewStorageEventArgs 表示在创建新存储文件或处理完成后触发的事件中使用的参数。

MimeItemCopyEventArgs 表示与将 MailMessage 对象从一个存储复制到另一个存储相关的事件参数,无论是在复制开始之前还是完成之后。

以下代码示例演示如何与 Mbox 文件交互,处理与这些文件相关的事件,并执行诸如将 Mbox 存储拆分为更小部分的操作,同时跟踪消息计数和部分计数:

messageCount = 0;
partCount = 0;

// Create an instance of MboxrdStorageReader
MboxLoadOptions lo = new MboxLoadOptions();
lo.setLeaveOpen(false);
MboxrdStorageReader mbox = new MboxrdStorageReader("my.mbox", lo);

// Subscribe to events
mbox.setMboxFileCreatedEventHandler(new NewStorageEventHandler() {
    public void invoke(Object sender, NewStorageEventArgs e) {
        System.out.println("New Mbox file created: " + e.getFileName());
        partCount++;
    }
});

mbox.setMboxFileFilledEventHandler(new NewStorageEventHandler() {
    public void invoke(Object sender, NewStorageEventArgs e) {
        System.out.println("Mbox file filled with messages: " + e.getFileName());
    }
});

mbox.setEmlCopiedEventHandler(new MimeItemCopyEventHandler() {
    public void invoke(Object sender, MimeItemCopyEventArgs e) {
        System.out.println("Message added to new Mbox file. Subject: " + e.getItem().getSubject());
        messageCount++;
    }
});

// Split the Mbox storage into smaller parts
mbox.splitInto(10000000, testOutPath, "Prefix");

// Output the final messageCount and partCount
System.out.println("Total messages added: " + messageCount);
System.out.println("Total parts created: " + partCount);