generate .mht file programmatically

回眸只為那壹抹淺笑 提交于 2019-12-01 00:43:02

问题


Does anybody know how to generate .mht file programmatically in C#, with embedded images in it? The thing is i have realised that .mht files are capable of embedding images in them, and this embedded images moves with the whole file(mht) when you change its location. And this file can be viewed on different browsers, including IE 6.

I was told to try Data Url Scheme technique. But it couldn't work because its not supported by other browsers. e.g IE 6.


回答1:


It is possible and has definitely been done by others - relevant material and libraries/source code:

  • RFC 2557 is the foundation of MHT format
  • VB.NET source code for generating MHT files from an URL
  • Chilkat MHT .NET library (commercial)
  • ASPNetMHT (commercial)
  • C# source code for generating MHT files from an URL
  • CDO.Message COM component, which exposes the IMessage interface



回答2:


In a Windows environment you can do this directly by means of the CDO.Message COM component, which exposes the IMessage interface
I dont know the details of importing COM objects in C# so I'll give you a quick example in C-like syntax:

IMsgObj = CreateObject("CDO.Message")   // create the object
IMsgObj.CreateMHTMLBody("http://www.example.com/") // convert the URL to MHTML
IMsgObj.GetStream().SaveToFile("output.mht")  // save it to a file

The CLSID of the CDO.Message components is {CD000001-8B95-11D1-82DB-00C04FB1625D}

Keep in mind though, that this component is meant for generating e-mail messages (.eml file extension rather than .mht) which means JavaScript files are not included. Otherwise they are roughly equivalent.




回答3:


I'd start with RFC 2557 so that I had some basic understanding of what I was working with. Then go look for code/libraries that deal with it.

To my knowledge there are no BCL classes to deal with MHTML.




回答4:


MSDN has a great article (June 2011) about how to create an MHT file using both CDO and System.Net.Mail. C# source code is included in-full. I would use this above the VB.Net or Google Code version.



来源:https://stackoverflow.com/questions/9999876/generate-mht-file-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!