tags:

views:

77

answers:

1

I'm porting some VBScript code which generates emails using the standard CDOSYS Message object. The Message oject has a property AutoGenerateTextBody which when true will cause it to automatically create the TextBody property value when you assign HTML to the HTMLBody property. Hence creating the typical text/plain and text/html alternatives in the message body.

However .NET appears to be missing this function. The MailMessage object does have the ability to create alternative views but there doesn't appear to be a way to easily create the text body content from the HTML content.

I'm not necessarily looking for an auto-magic option but I do need a solution to taking what is an HTML string and converting it to a reasonable plain text representation. Just dropping all the HTML markup doesn't cut it.

Is there a tool buried somewhere in the existing .NET framework that can do this?

A: 

I'm not aware of anything in the .NET framework itself, but you could use CDO to do the conversion for you. Admittedly it feels like a bit of a dirty hack, but does the job!

Add a reference to the "Microsoft CDO for Windows 2000 Library" (in the COM tab of the "Add Reference" dialog) and away you go:

public string GetTextBody(string htmlBody)
{
    CDO.Message msg = new CDO.Message();
    msg.AutoGenerateTextBody = true;
    msg.HTMLBody = htmlBody;

    return msg.TextBody;
}
LukeH
Thanks, but no thanks. ;) I'd rather write my one HTML->Text conversion than add a dependency on the CDOSYS just achieve this operation.
AnthonyWJones
@Anthony: Yep, I agree completely. Just thought I'd mention the possibility.
LukeH