12

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.

7
  • I've been there - while its very possible to do this, even writing it yourself - don't go there. MHT is very proprietary and NOT supported by most modern browsers. I would look into alternatives first Commented Apr 3, 2012 at 19:20
  • 1
    I think the easiest way would be to save a few simple web pages as .mht with IE and see how it does this.
    – L.B
    Commented Apr 3, 2012 at 19:20
  • 1
    I tried to export .mht file on IE. It seems to implement Data Url Scheme technique. E.g if found the following code in the exported file(mht) Commented Apr 3, 2012 at 19:33
  • 1
    Content-Type: image/gif Content-Transfer-Encoding: base64 Content-Location: oakdome.com/lab/wp-content/step-two-create-new-connection.gif R0lGODlhwgEuAfcAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwMDcwKbK8AIAAgABRwAAbQsrVQ0v bjsARTsAcCg3Tyc6bBBVDxFWOA1wDR1pKidTFTRaMCptDyx0MQVPTA5FdxhlSw9qbzNHVS9Ocyxz RydseVQABFkBZFNhH2BlaQcWigYWrg46kwc6qzg8lSo4tQAczgU20gg15Cc10QJFkhNMtABqngBu Commented Apr 3, 2012 at 19:33
  • 1
    @chosenOneThabs MHT is based on RFC 2557 - see answer(s) below...
    – Yahia
    Commented Apr 3, 2012 at 19:34

4 Answers 4

9

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

4
  • I tried the above. It seems like they are able to convert an html file to mht. This works if access an html file from the web. But if you access the html file from your local drive then the conversion won't work. The above sources makes use of this method : CreateMHTMLBody(URL, CDO.CdoMHTMLFlags.cdoSuppressNone, "", ""); Commented Apr 7, 2012 at 16:04
  • The reason why i'm trying to embed image in html file is because i'm creating a windows form application that has to export a report with images. Is there a way to accomplish without using commercial libraries? Commented Apr 7, 2012 at 16:08
  • @chosenOneThabs the only way to do that without using commercial libraries is to implement the complete format (which is only partially documented) yourself.
    – Yahia
    Commented Apr 7, 2012 at 16:46
  • Oh i finally decided to make use of Visual studio's ReportViewer Control, because i realized that this issue was going to take me some time to solve, and i'm having a deadline. But thanks alot, at least i learned about how MHT files work. Commented Apr 8, 2012 at 22:33
8

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.

2
  • is valid for the html file from local drive?
    – Kiquenet
    Commented Oct 22, 2016 at 9:23
  • @Kiquenet, it should work. Just pass a file path instead of a URL
    – GetFree
    Commented Oct 22, 2016 at 18:50
3

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.

2
  • 1
    Notes: The main disadvantages of using CDO are: COM Interop! and Does not work in 64 bit systems (x64)
    – Kiquenet
    Commented Oct 22, 2016 at 9:07
  • Notes: This sample (MSDN article) does not include code for handling css background images and iframes
    – Kiquenet
    Commented Oct 22, 2016 at 9:09
1

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
  • I tried the above. It seems like they are able to convert an html file to mht. This works if access an html file from the web. But if you access the html file from your local drive then the conversion won't work. The above sources makes use of this method : CreateMHTMLBody(URL, CDO.CdoMHTMLFlags.cdoSuppressNone, "", ""); Commented Apr 7, 2012 at 16:07
  • The reason why i'm trying to embed image in html file is because i'm creating a windows form application that has to export a report with images. Is there a way to accomplish without using commercial libraries? Commented Apr 7, 2012 at 16:08
  • Create the report as a template. Export from ie. do search/replace to get report content in.
    – Sam Axe
    Commented Apr 8, 2012 at 5:20
  • Oh i finally decided to make use of Visual studio's ReportViewer Control, because i realized that this issue was going to take me some time to solve, and i'm having a deadline. But thanks alot, at least i learned about how MHT files work. Commented Apr 8, 2012 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.