开发者

Is there an equivalence to CDOSYS AutoGenerateTextBody in .NET

开发者 https://www.devze.com 2022-12-08 16:55 出处:网络
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 cre

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 t开发者_开发技巧here 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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号