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 mail storage file. This article shows how to read the messages from Thunderbird email storage:

  1. Open the Thunderbird 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.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Thunderbird();
// Open the storage file with FileStream
FileStream stream = new FileStream(dataDir + "ExampleMbox.mbox", FileMode.Open, FileAccess.Read);
// Create an instance of the MboxrdStorageReader class and pass the stream
MboxrdStorageReader reader = new MboxrdStorageReader(stream, false);
// Start reading messages
MailMessage message = reader.ReadNextMessage();
// Read all messages in a loop
while (message != null)
{
// Manipulate message - show contents
Console.WriteLine("Subject: " + message.Subject);
// Save this message in EML or MSG format
message.Save(message.Subject + ".eml", SaveOptions.DefaultEml);
message.Save(message.Subject + ".msg", SaveOptions.DefaultMsgUnicode);
// Get the next message
message = reader.ReadNextMessage();
}
// Close the streams
reader.Dispose();
stream.Close();

Writing Messages

The MboxrdStorageWriter class provides the facility to write new messages to Thunderbird 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 write messages to Thunderbird mail storage.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Open the storage file with FileStream
FileStream stream = new FileStream(dataDir + "ExampleMbox.mbox", FileMode.Open, FileAccess.Write);
// Initialize MboxStorageWriter and pass the above stream to it
MboxrdStorageWriter writer = new MboxrdStorageWriter(stream, false);
// Prepare a new message using the MailMessage class
MailMessage message = new MailMessage("from@domain.com", "to@domain.com", Guid.NewGuid().ToString(), "added from Aspose.Email");
message.IsDraft = false;
// Add this message to storage
writer.WriteMessage(message);
// Close all related streams
writer.Dispose();
stream.Close();

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.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Thunderbird();
using (FileStream stream = new FileStream(dataDir + "ExampleMbox.mbox", FileMode.Open, FileAccess.Read))
using (MboxrdStorageReader reader = new MboxrdStorageReader(stream, false))
{
Console.WriteLine("Total number of messages in Mbox file: " + reader.GetTotalItemsCount());
}

Get Current Message Size

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
string dataDir = RunExamples.GetDataDir_Thunderbird();
using (FileStream stream = new FileStream(dataDir + "ExampleMbox.mbox", FileMode.Open, FileAccess.Read))
using (MboxrdStorageReader reader = new MboxrdStorageReader(stream, false))
{
MailMessage msg;
while ((msg = reader.ReadNextMessage()) != null)
{
long currentDataSize = reader.CurrentDataSize;
msg.Dispose();
}
}
s