Rendering an e-mail message via WPF

Hello,

I’m probably guilty of using a sledgehammer to crack a nut, but I’m keen that my company get Aspose.Words and perhaps Aspose.Network as well. So if I can get this working, I’ll have a stronger case to make.

The scenario is a WPF application onto which users can drag and drop e-mails from Outlook. Once I get the basics working, I’ll be showing all saved e-mails in a side pane, within a listbox, as appropriate for the main content of the application. For now, I’m in a proof of concept stage and just trying to get a message displayed in a simple WPF window.

So far, I’ve got to the stage where a message can be dragged and dropped, and having dropped, the message is saved to a backend SQL Server 2005 database. Thanks to the code I found (Drag'n'drop one or more mails from Outlook to C# WPF application - Stack Overflow) and the related link (Outlook Drag and Drop in C# - CodeProject), I’m saving in msg format. For those who follow, the former gives the required changes so that the backend OutlookDataObject code works in WPF.

Then I’ve retrieved the message from the database and assigned it to a MapiMessage object - which is where Aspose.Network comes in. Works well - breaking into the code, I can see all the message attributes are there, along with the content. Here’s the code for that stage in case it helps anyone:

SqlDataReader reader = cmdSQL.ExecuteReader(CommandBehavior.CloseConnection);

if (reader != null)
if (reader.HasRows)
{
while (reader.Read())
{
SqlBytes msgBytes = reader.GetSqlBytes(0);
MapiMessage msg = MapiMessage.FromStream(msgBytes.Stream);
MsgResults.AppendText(msg.Body);
}
}

… where MsgResults is a WPF RichTextBox (.NET v4).

But I’m stuck on how to render this retrieved message. I’d like it to show just as it would in Outlook, with text colours and other formatting. I’ve tried a RichTextBox but it doesn’t want to know unless I just show the message body - and when I do that, all formatting is lost. I feel confident that the formatting is there since the code samples referenced above have you save a dropped message off to a file. When opened, it is faithful in all its details including headers, subject, attachments etc.

So the question is what control to use to render the retrieved message. Any guidance gratefully received.

Thanks and regards

Sebastian Crewe

Hi Sebastian,


Thanks for considering Aspose.
  1. I am sorry, we do not have any rendering control for displaying the messages at the moment. The library only provides the API for handling the messages in a .NET program.

    However, we do have a Windows Forms based control for enabling drag/drop messages from Outlook and saving it in MSG format to disk. But, I am sorry, it does not work with WPF. More details at http://www.aspose.com/documentation/.net-components/aspose.network-for-.net/using-aspose-outlook-control-to-drag-drop-outlook-messages-to-windows-forms.html.

  2. Regarding the formatting of the message, the MapiMessage.BodyRtf property can get the correct formatting, if the message is prepared in Outlook using “Options” – “Rich Text”. If the message is prepared using the Html formatting, it will not be displayed correctly.

Thank you for your quick response. Ref your point 1, saving in msg format is not a problem; the articles I mentioned in my first post give guidance on how to achieve that. I am able to respond to the drop, save the message to a SQL database and retrieve it. The problem is how to display it with all its formatting intact.

Regarding your point 2, I can try playing with the BodyRtf property for messages in that format. Perhaps Apose can consider BodyHtml in a future release?

In the meantime, is there a way I could convert from an HTML message to RTF format before saving the dropped message to the database, perhaps using Aspose Words? Then I could make separate display fields for the To, From and Subject elements, and render the body in (presumably) an RTF control.

With thanks and regards

Sebastian

Hi,


You can alternatively get the body text in HTML format by loading the message in MailMessage class. And in a WPF application, it can be displayed in a WebBrowser control.

MailMessage message = MailMessage.Load(“test.msg”, MessageFormat.Msg);
webbrowser1.NavigateToString(message.HtmlBody);