简体   繁体   中英

Is there an equivalence to CDOSYS AutoGenerateTextBody in .NET

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?

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM