11

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.

Sam Axe
  • 31,472
  • 7
  • 52
  • 80
chosenOne Thabs
  • 1,252
  • 3
  • 18
  • 34
  • 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 – BrokenGlass Apr 03 '12 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 Apr 03 '12 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) – chosenOne Thabs Apr 03 '12 at 19:33
  • 1
    Content-Type: image/gif Content-Transfer-Encoding: base64 Content-Location: http://www.oakdome.com/lab/wp-content/step-two-create-new-connection.gif R0lGODlhwgEuAfcAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwMDcwKbK8AIAAgABRwAAbQsrVQ0v bjsARTsAcCg3Tyc6bBBVDxFWOA1wDR1pKidTFTRaMCptDyx0MQVPTA5FdxhlSw9qbzNHVS9Ocyxz RydseVQABFkBZFNhH2BlaQcWigYWrg46kwc6qzg8lSo4tQAczgU20gg15Cc10QJFkhNMtABqngBu – chosenOne Thabs Apr 03 '12 at 19:33
  • 1
    @chosenOneThabs MHT is based on RFC 2557 - see answer(s) below... – Yahia Apr 03 '12 at 19:34
  • 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? – chosenOne Thabs Apr 07 '12 at 16:07
  • 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. – chosenOne Thabs Apr 08 '12 at 22:35

4 Answers4

9

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

dns
  • 2,564
  • 1
  • 22
  • 30
Yahia
  • 67,016
  • 7
  • 102
  • 131
  • 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, "", ""); – chosenOne Thabs Apr 07 '12 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? – chosenOne Thabs Apr 07 '12 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 Apr 07 '12 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. – chosenOne Thabs Apr 08 '12 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.

GetFree
  • 34,030
  • 17
  • 70
  • 101
2

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.

Jason S
  • 1,089
  • 10
  • 9
  • 1
    `Notes:` ***The main disadvantages of using CDO are:*** _COM Interop!_ and _Does not work in 64 bit systems (x64)_ – Kiquenet Oct 22 '16 at 09:07
  • `Notes:` *This sample (MSDN article) does not include code for handling css background images and iframes* – Kiquenet Oct 22 '16 at 09: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.

Sam Axe
  • 31,472
  • 7
  • 52
  • 80
  • 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, "", ""); – chosenOne Thabs Apr 07 '12 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? – chosenOne Thabs Apr 07 '12 at 16:08
  • Create the report as a template. Export from ie. do search/replace to get report content in. – Sam Axe Apr 08 '12 at 05: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. – chosenOne Thabs Apr 08 '12 at 22:34