Extract MBOX Messages email address to CSV

Hi, Is it any way to extract message information of MBOX files like email addresses (to, cc, bcc, to), subject, body, date and time, etc into CSV or other document files?

@geetasoni

To read and write MBOX/MBOXRD, please read the following article.
Programming with Thunderbird

No, actually I want to extract MBOX file email data to a CSV file like received email address only sent email address only into a tabular format like Microsoft Excel.

Hello, @geetasoni

We don’t have a ready-made feature to convert mbox to csv, but you can easily do it yourself with a few lines of code.
Here is the code for an example:

    using (var mboxReader = new MboxrdStorageReader("my.mbox", false))
    {
        using var csvFile = File.OpenWrite("my.csv");
        using var csvWriter = new StreamWriter(csvFile);
        
        // write a csv table header
        csvWriter.WriteLine("From, To, BCC, CC, Date, Subject");
        
        // read the messages from mbox and write some message fields to csv
        foreach (var message in mboxReader.EnumerateMessages())
        {
            var from = message.From;
            var to = message.To;
            var bcc = message.Bcc;
            var cc = message.CC;
            var date = message.Date;
            // take care to escape the ' " ' character inside the text according to the csv spec
            var subject = $"\"{message.Subject.Replace("\"", "\"\"")}\"";
            
            // create a csv record. A comma as separator
            var record = string.Join(',', from, to, bcc, cc, date, subject);
            
            // write record to csv
            csvWriter.WriteLine(record);
        }
        
        csvWriter.Flush();
    }

Thanks.

I tried this code but output CSV file was generated corrupted. However, before csvWrite.WriteLine. data will come out but after CSV Writer class file was corrupted. Can you please check the code again. Thank you.

Hello, @geetasoni

You are probably facing the issue that hasn’t related to Aspose.Email.
I just gave above a basic example that explains the process.
Aspose.Email is responsible for reading messages from mbox and retrieving the appropriate message fields (Subject, Body, To, From etc).
Next, you have to provide for writing those fields to csv yourself according to the format specification.
You should also be aware that there are several interpretations of the csv format, different in an delimiter used to separate columns.
Also, you can use a third party library to write the csv file. For example, GitHub - mgholam/fastCSV: Fast CSV reader writer in c#.
Here is a code example using this library.

List<MailMessage> mailMessageList = new List<MailMessage>();

using (var mbox = new MboxrdStorageReader("my.mbox", new MboxLoadOptions()))
{
    foreach (var message in mbox.EnumerateMessages())
    {
        mailMessageList.Add(message);
    }
}

fastCSV.WriteFile<MailMessage>(
    "my.csv",   // filename
    new string[] { "From", "To", "BCC", "CC", "Date", "Subject", "Body" }, // csv header
    ',',               // delimiter
    mailMessageList,              // list of emails to save
    (msg, col) =>          // from object function 
    {
        col.Add(msg.From);
        col.Add(msg.To);
        col.Add(msg.Bcc);
        col.Add(msg.CC);
        col.Add(msg.Date.ToString(CultureInfo.CurrentCulture));
        col.Add(msg.Subject);
        col.Add(msg.Body.Replace("\r\n", " "));
    });

Thank you.