MBOX to HTML conversion

Hello,

Is There a way to read and convert from a MBOX file to HTML file using Aspose.email?

Thank & regards!
Mantu Malakar

Hi Mantu,

Thank you for posting your inquiry.

You can use MboxrdStorageReader.ReadNextMessage() to read messages from an MBOX file. This will give you access to the MailMessage object, which can then be used for saving to various supported formats such as HTML, MHTML etc. Please see sample code below to read an MBOX file and convert messages to HTML,

// Open the storage file with FileStream

using (FileStream stream = new FileStream(dataDir + @"Starred.mbox", FileMode.Open, FileAccess.Read))

{

// Create an instance of MboxrdStorageReader class and pass the stream

Aspose.Email.Formats.Mbox.MboxrdStorageReader reader = new Aspose.Email.Formats.Mbox.MboxrdStorageReader(stream, false);

// Start reading messages

MailMessage message = reader.ReadNextMessage();

int i = 0;

// Read all messages in a loop

while (message != null)

{

// Save this message in HTML format

message.Save("HTML_out_ " + i + ".html", SaveOptions.DefaultHtml);

// Read next message

message = reader.ReadNextMessage();

i++;

}

}


Please try above code at your end and let us know if you have any further query in this regard.<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin-top:0in; mso-para-margin-right:0in; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0in; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;}

<![endif]–>

Hello,
thanks a lot.
but it is creating only the body of the mail. i need all the content like sender-name, sender-email, date etc.
So i need to read all the data from mbox file to create this.
thankz & regards
Mantu Malakar

Hi Mantu,

Thanks for the feedback. You may use HtmlSaveOptions to save the message header as part of the HTML which includes sender email address, receiver email addresses, subject etc. Please see sample code below to achieve the same,

// Open the storage file with FileStream

using (FileStream stream = new FileStream(dataDir + @"Starred.mbox", FileMode.Open, FileAccess.Read))

{

// Create an instance of MboxrdStorageReader class and pass the stream

Aspose.Email.Formats.Mbox.MboxrdStorageReader reader = new Aspose.Email.Formats.Mbox.MboxrdStorageReader(stream, false);

// Start reading messages

MailMessage message = reader.ReadNextMessage();

int i = 0;

// Read all messages in a loop

while (message != null)

{

// Save as html with header

HtmlSaveOptions htmlSaveOptions = new HtmlSaveOptions

{

HtmlFormatOptions = HtmlFormatOptions.WriteHeader | HtmlFormatOptions.WriteCompleteEmailAddress

};

// Save this message in HTML format

message.Save("HTML_out_ " + i + ".html", htmlSaveOptions);

// Read next message

message = reader.ReadNextMessage();

i++;

}

}


Please try the above code and let us know if you have any further query in this regard.

Hello ,
During conversion Mbox to html how to save the attachments file of each mail into folder ??
Thanks & Regards
Mantu Malakar

Hi Mantu,

You can use MailMessage.Attachments property to retrieve all attachments in a message. You can then iterate this attachment collection and save each to disk using Attachment.Save. Please see sample code below:

// Open the storage file with FileStream

using (FileStream stream = new FileStream(dataDir + @"Starred.mbox", FileMode.Open, FileAccess.Read))

{

// Create an instance of MboxrdStorageReader class and pass the stream

Aspose.Email.Formats.Mbox.MboxrdStorageReader reader = new Aspose.Email.Formats.Mbox.MboxrdStorageReader(stream, false);

// Start reading messages

MailMessage message = reader.ReadNextMessage();

// Read all messages in a loop

while (message != null)

{

foreach (Attachment attachment in message.Attachments)

{

attachment.Save(dataDir + attachment.Name);

}

// Read next message

message = reader.ReadNextMessage();

}

}


Please try above at your end and let us know your feedback.