Programming with Thunderbird

Reading Messages

Mozilla Thunderbird is an open-source, cross-platform email client, developed by the Mozilla Foundation. It stores emails in its own file structure, managing messages indices and subfolders through proprietary file formats. Aspose.Email can work with Thunderbird mail storage structures. The MboxrdStorageReader class lets developers read messages from Mozilla Thunderbird’s mail storage file. This article shows how to read the messages from Thunderbird email storage:

  1. Open the Thunderbird’s storage file in FileStream.
  2. Create an instance of the MboxrdStorageReader class and pass the above stream to the constructor.
  3. Call ReadNextMessage() to get the first message.
  4. Use the same ReadNextMessage() in a while loop to read all the messages.
  5. Close all the streams.

The following code snippet shows you how to read all the messages from a Thunderbird mail storage.

//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]);
        }
    }
}

Writing Messages

The MboxrdStorageWriter class provides the facility to write new messages to Thunderbird’s mail storage file. To write messages:

  1. Open the Thunderbird storage file in FileStream.
  2. Create an instance of the MboxrdStorageWriter class and pass the above stream to the constructor.
  3. Prepare a new message using the MailMessage class.
  4. Call the WriteMessage() method and pass the above MailMessage instance to add the message to Thunderbird storage.
  5. Close all streams.

The following code snippet shows you how to writes messages to Thunderbird’s mail storage.

//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]);
    }
}

Getting Total Number of Messages from MBox File

The MboxrdStorageReader class provides the capability to read the number of items available in an MBox file. This can be used to develop applications for showing the progress of activity while processing such a file.

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

Get Current Message Size

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);
    }
}

Set Preferred Text Encoding when Loading Mbox Files

The setPreferredTextEncoding(Charset value) method of the MboxLoadOptions class gets or sets preferred encoding for messages. Create a reader for the Mbox file with specified load options and your messages with the encoded content will be correctly read and processed. The following code sample shows how to implement this feature into a project:

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