Click here to Skip to main content
11,641,416 members (63,154 online)
Click here to Skip to main content

Convert HTML to MHTML using ASP.NET

, 14 Jun 2004 280.1K 4.1K 69
Rate this:
Please Sign up or sign in to vote.
An article on how to convert a html document with images to a mhtml document

Introduction

Ever wanted to make a report out of an html document and have it sent to the client for offline use in Word or Excel? An RFC - compliant Multipart MIME Message (mhtml web archive) is one single file containing all related material such as linked documents and images serialized to their Base64 inline encoding representations. There is no native support for creating mhtml archives in .NET but thanks to the Windows CDO library this is easy accomplished.

The code

The projects contains 3 classes; mht, mhtImage and mhtImageCollection. The mht class contains the conversion functions like convertWebControlToMHTString which takes a webControl and a collection of images and returns a string representation of the created mht archive. Use this function when the converting webControl is dependent on user specific Session and Application variables for rendering or when you use dynamically created images.

Public Function convertWebControlToMHTString(ByVal control As WebControl, _
  ByVal MHTimages As mhtImageCollection) As String
  'Render WebControl to html
  Dim html As String = getHtml(control)

  'If WebControl has images, make the html Word compatible
  If Not MHTimages Is Nothing Then
    fixImageLocation(html, MHTimages)
  End If

  Dim msg As New CDO.MessageClass
  Dim stm As ADODB.Stream = Nothing
  Dim MS As System.IO.MemoryStream = Nothing

  Dim iBp As CDO.IBodyPart

  'Make a multipart mhtml document
  Dim mainBody As CDO.IBodyPart
  mainBody = msg
  mainBody.ContentMediaType = "multipart/related"

  'Make the html part of the document
  iBp = mainBody.AddBodyPart()
  iBp.ContentMediaType = "text/html"
  iBp.ContentTransferEncoding = "quoted-printable"
  stm = iBp.GetDecodedContentStream
  stm.WriteText(html)
  stm.Flush()

  'Make the image parts of the document
  If Not MHTimages Is Nothing Then
    Dim oMhtImage As mhtImage
    For Each oMhtImage In MHTimages
      iBp = mainBody.AddBodyPart()
      With iBp
        .ContentMediaType = "image/" + _
 oMhtImage.ImageFormat.ToString().ToLower()
        .ContentTransferEncoding = "base64"

        'ContentLocation must be the same as in the 
        'html part to make them linked
        .Fields.Append("urn:schemas:mailheader:content-location", _
    DataTypeEnum.adBSTR, , , oMhtImage.ContentLocation)
        .Fields.Update()
        .Fields.Refresh()
      End With

      Try
        MS = New System.IO.MemoryStream
        oMhtImage.Image.Save(MS, oMhtImage.ImageFormat)
        Dim bytearray As Byte() = MS.ToArray()
        stm = iBp.GetDecodedContentStream
        stm.Write(bytearray)
        stm.Flush()
      Finally
        MS.Close()
        stm.Close()
      End Try
    Next
  End If

  stm = mainBody.GetStream()
  Return stm.ReadText(stm.Size)
End Function
      

The convertWebPageToMHTString function converts an html document from a specific URL to a mht archive, all images included. Use this function for public html documents not dependent on user specific Session and Application variables.

Public Function convertWebPageToMHTString(ByVal url As String) As String
    Dim msg As New CDO.MessageClass
    Dim stm As ADODB.Stream = Nothing

    Try
        msg.MimeFormatted = True
        msg.CreateMHTMLBody(url, CDO.CdoMHTMLFlags.cdoSuppressNone, "", "")
        stm = msg.GetStream()
        Return stm.ReadText(stm.Size)
    Finally
        stm.Close()
    End Try
End Function
    

The fixImageLocation appends the string "http://" at the beginning of each ContentLocation if not already there, for Word compliance

Private Sub fixImageLocation( _
      ByRef html As String, ByRef MHTimages As mhtImageCollection)
    Dim curContentLocation As String
    Dim curIndex As Integer
    Dim oMhtImage As mhtImage
    For Each oMhtImage In MHTimages
        curContentLocation = oMhtImage.ContentLocation
        If curContentLocation.IndexOf(":") = -1 Then
            curIndex = html.IndexOf(curContentLocation)
            While curIndex <> -1
                html = html.Insert(curIndex, "http://")
                curIndex = html.IndexOf(curContentLocation, curIndex + _
   curContentLocation.Length)
            End While
            oMhtImage.ContentLocation = "http://" + curContentLocation
        End If
    Next
End Sub
    

The mhtImage class contains image information. Property Image contains the actual image. Property ContentLocation contains the path to the image, must be exactly the same as the source for the image in the html part. Property ImageFormat contains the image format (jpg, gif, bmp...)

The mhtImageCollection class contains a collection of mhtImages.

Using the code

Example on how to make a mht archive from a Panel webControl containing one image.

Dim oMhtCol As New mhtImageCollection
oMhtCol.add(New mhtImage(System.Drawing.Image.FromFile( _
  Server.MapPath("/mhtml/images/myComputer.jpg")), _
  "images/myComputer.jpg", System.Drawing.Imaging.ImageFormat.Jpeg))
sendMHTFile(ConvertWebControlToMHTString(Panel1, oMhtCol), "myFirstMht.mht")
    

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Magnus_
Web Developer
Sweden Sweden
Software developer

You may also be interested in...

Comments and Discussions

 
Generalslow code Pin
eerisken1-Aug-05 0:44
membereerisken1-Aug-05 0:44 
GeneralSaving MHT files in IE Pin
brianp200424-Mar-05 4:42
memberbrianp200424-Mar-05 4:42 
GeneralRe: Saving MHT files in IE Pin
Partenon24-Mar-05 6:03
memberPartenon24-Mar-05 6:03 
GeneralCreateMHTMLBody Pin
kikde7-Mar-05 1:05
memberkikde7-Mar-05 1:05 
GeneralEmbedding CSS Style Sheets Pin
brianp200424-Feb-05 19:59
memberbrianp200424-Feb-05 19:59 
GeneralRe: Embedding CSS Style Sheets Pin
Partenon25-Feb-05 10:51
memberPartenon25-Feb-05 10:51 
GeneralRe: Embedding CSS Style Sheets Pin
brianp200425-Feb-05 13:29
memberbrianp200425-Feb-05 13:29 
GeneralMHTML files not opening Pin
Anonymous24-Feb-05 5:24
sussAnonymous24-Feb-05 5:24 
GeneralRe: MHTML files not opening Pin
pashudizhu28-Aug-06 2:28
memberpashudizhu28-Aug-06 2:28 
GeneralEasier way Pin
Anonymous22-Nov-04 5:12
sussAnonymous22-Nov-04 5:12 
GeneralRe: Easier way Pin
Rob Holmes5-May-05 22:08
memberRob Holmes5-May-05 22:08 
Generalformat incorrect Pin
fvelazco11-Nov-04 13:04
sussfvelazco11-Nov-04 13:04 
GeneralRe: format incorrect Pin
Partenon13-Nov-04 0:03
memberPartenon13-Nov-04 0:03 
GeneralCreating .mht files with background music (help, pleez) Pin
DaiSekkou3-Nov-04 18:14
sussDaiSekkou3-Nov-04 18:14 
Generalregarding conversion to mht Pin
FarrukhNaeem24-Oct-04 10:35
memberFarrukhNaeem24-Oct-04 10:35 
QuestionHow to write the line &quot;Fields.Append...&quot; in c# Pin
rnzjh15-Oct-04 8:59
memberrnzjh15-Oct-04 8:59 
AnswerRe: How to write the line &quot;Fields.Append...&quot; in c# Pin
LeviJackson12-Aug-05 9:51
memberLeviJackson12-Aug-05 9:51 
GeneralRe: How to write the line &quot;Fields.Append...&quot; in c# Pin
Beginner_C#17-Aug-06 5:19
memberBeginner_C#17-Aug-06 5:19 
GeneralRe: How to write the line &quot;Fields.Append...&quot; in c# Pin
3R1CK_MX22-Dec-06 6:58
member3R1CK_MX22-Dec-06 6:58 
GeneralConvert to MHT in C# Pin
shaanr30-Sep-04 10:01
sussshaanr30-Sep-04 10:01 
Generalembed images in Word 2000 Pin
Anonymous24-Aug-04 2:13
sussAnonymous24-Aug-04 2:13 
GeneralRe: embed images in Word 2000 Pin
Partenon14-Sep-04 2:51
memberPartenon14-Sep-04 2:51 
GeneralRe: embed images in Word 2000 Pin
Partenon16-Oct-04 1:24
memberPartenon16-Oct-04 1:24 
GeneralProblem running Example Pin
WorthR18-Aug-04 13:04
memberWorthR18-Aug-04 13:04 
GeneralError trying to build/run examples Pin
WorthR18-Aug-04 13:00
memberWorthR18-Aug-04 13:00 
GeneralNice example Pin
Zygnus15-Aug-04 21:23
memberZygnus15-Aug-04 21:23 
GeneralForms Authentication Pin
Noel H22-Jul-04 20:13
sussNoel H22-Jul-04 20:13 
GeneralRe: Forms Authentication Pin
Partenon22-Jul-04 20:36
memberPartenon22-Jul-04 20:36 
Generalconver mht to html Pin
Anonymous9-Jul-04 23:22
sussAnonymous9-Jul-04 23:22 
GeneralRe: conver mht to html Pin
Anonymous9-Jul-04 23:36
sussAnonymous9-Jul-04 23:36 
GeneralRe: conver mht to html Pin
geblack1-Jul-05 7:45
sussgeblack1-Jul-05 7:45 
QuestionMHTML, help???? Pin
Greg Mc9-Jul-04 3:17
memberGreg Mc9-Jul-04 3:17 
AnswerRe: MHTML, help???? Pin
Partenon9-Jul-04 12:13
memberPartenon9-Jul-04 12:13 
GeneralRe: MHTML, help???? Pin
Greg Mc14-Jul-04 1:22
memberGreg Mc14-Jul-04 1:22 
GeneralRe: MHTML, help???? Pin
Partenon14-Jul-04 7:34
memberPartenon14-Jul-04 7:34 
Generalproblem with embedding the images Pin
ashish_sharma30-Jun-04 19:44
memberashish_sharma30-Jun-04 19:44 
GeneralRe: problem with embedding the images Pin
Kalle Johansson1-Jul-04 23:39
sussKalle Johansson1-Jul-04 23:39 
GeneralBack to HTML Pin
Gfw21-Jun-04 14:56
memberGfw21-Jun-04 14:56 
GeneralRe: Back to HTML Pin
Partenon23-Jun-04 9:42
memberPartenon23-Jun-04 9:42 
GeneralRe: Back to HTML Pin
Anonymous15-Jul-04 23:54
sussAnonymous15-Jul-04 23:54 
GeneralRe: Back to HTML Pin
Anonymous23-Jul-04 11:11
sussAnonymous23-Jul-04 11:11 
GeneralRe: Back to HTML Pin
Anonymous24-Feb-05 5:49
sussAnonymous24-Feb-05 5:49 
Questionwhy vb? Pin
diaodiao14-Jun-04 22:45
memberdiaodiao14-Jun-04 22:45 
AnswerRe: why vb? Pin
theJazzyBrain14-Jun-04 23:07
membertheJazzyBrain14-Jun-04 23:07 
GeneralWhy ASP.NET Pin
Uwe Keim15-Jun-04 2:59
sitebuilderUwe Keim15-Jun-04 2:59 
GeneralRe: Why ASP.NET Pin
theJazzyBrain15-Jun-04 4:46
membertheJazzyBrain15-Jun-04 4:46 
AnswerRe: why vb? Pin
issigonis23-Jun-04 11:05
memberissigonis23-Jun-04 11:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150731.1 | Last Updated 15 Jun 2004
Article Copyright 2004 by Magnus_
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid