Convert HTML to MHTML | C#

MHTML combines normal HTML with external resources like images, animations, audio, etc., into one file with the .mht file extension. MHTML uses the MIME email protocol to combine items into a single web page archive file. With MHTML, the archival of online web pages becomes much easier and less cluttered.

In this article, you will find information on how to convert HTML to MHTML and how to use MHTMLSaveOptions.

Text “Banner HTML to MHTML Converter”

HTML to MHTML by a single line of code

The static methods of the Converter class are primarily used as the easiest way to convert an HTML code into various formats. You can convert HTML to MHTML in your C# application literally with a single line of code!

using System.IO;
using Aspose.Html.Converters;
using Aspose.Html.Saving;
...
    // Invoke the ConvertHTML method to convert the HTML code to MHTML           
    Converter.ConvertHTML(@"<h1>Hellow, Word!</h1>", ".", new MHTMLSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.mht"));

Convert HTML to MHTML

Converting a file to another format using the ConvertHTML() method is a sequence of operations among which document loading and saving. The next example explains how to convert HTML to MHTML by line by line:

  1. Load the HTML file using HTMLDocument class.
  2. Create an instance of the MHTMLSaveOptions class.
  3. Use the ConvertHTML() method of Converter class to save HTML as an MHTML file. You need to pass the HTMLDocument, MHTMLSaveOptions, and output file path to the ConvertHTML() method to convert HTML to MHTML.
using System.IO;
using Aspose.Html;
using Aspose.Html.Converters;
using Aspose.Html.Saving;
...
    // Prepare a path to a source HTML file
    string documentPath = Path.Combine(DataDir, "drawing.html");

    // Prepare a path for converted file saving 
    string savePath = Path.Combine(OutputDir, "drawing-output.mht");
    
    // Initialize an HTML document from the file
    using var document = new HTMLDocument(documentPath);
    
    // Initialize MHTMLSaveOptions 
    var options = new MHTMLSaveOptions();
    
    // Convert HTML to MHTML
    Converter.ConvertHTML(document, options, savePath);

Save Options

Aspose.HTML allows converting HTML to MHTML using default or custom save options. MHTMLSaveOptions usage enables you to customize the rendering process; you can specify the PageUrlRestriction, ResourceUrlRestriction, MaxHandlingDepth, etc.

PropertyDescription
PageUrlRestrictionThis property gets or sets restrictions applied to URLs of handled pages. The default value is RootAndSubFolders.
ResourceUrlRestrictionGets or sets restrictions applied to URLs of handled resources such as CSS, js, images, etc. The default is SameHost.
MaxHandlingDepthGets or sets the maximum depth of pages that will be handled.

Convert HTML to MHTML using MHTMLSaveOptions

To convert HTML to MHTML with MHTMLSaveOptions specifying, you should follow a few steps:

  1. Load or prepare an HTML file.
  2. Create a new MHTMLSaveOptions object.
  3. Use the ConvertHTML() method of the Converter class to save HTML as a MHTML file. You need to pass the HTMLDocument, MHTMLSaveOptions, and output file path to the ConvertHTML() method to convert HTML to MHTML.

The following example shows how to use MHTMLSaveOptions and create an MHTML file with custom save options:

using System.IO;
using Aspose.Html;
using Aspose.Html.Converters;
using Aspose.Html.Saving;
...
     // Prepare HTML code with a link to another file and save it to the file as 'document.html'
     var code = "<span>Hello, World!!</span> " +
                "<a href='document2.html'>click</a>";
     File.WriteAllText("document.html", code);

     // Prepare HTML code and save it to the file as 'document2.html'
     code = @"<span>Hello, World!!</span>";
     File.WriteAllText("document2.html", code);
     
     string savePath = Path.Combine(OutputDir, "output-options.mht");
    
     // Change the value of the resource linking depth to 1 in order to convert document with directly linked resources
     var options = new MHTMLSaveOptions()
     {
         ResourceHandlingOptions =
         {
             MaxHandlingDepth = 1
         }
     };
    
     // Convert HTML to MHTML
     Converter.ConvertHTML("document.html", options, savePath);

The MHTMLSaveOptions() constructor initializes an instance of the MHTMLSaveOptions class that is passed to ConvertHTML() method. The ConvertHTML() method takes the document, options, output file path savePath and performs the conversion operation. The MHTMLSaveOptions class provides numerous properties that give you full control over a wide range of parameters and improve the process of converting HTML to MHTML format.

In the above example, we use the property MaxHandlingDepth = 1 means that only pages directly referenced from the saved document will be handled.