Convert HTML to MHTML – C# Examples

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.

Online HTML Converter

You can convert HTML to MHTML with Aspose.HTML for .NET API in real time. First, load an HTML file from your local drive and then run the example. In this example, the save options are set by default. You will immediately receive the conversion result as a separate file.

                
            

If you want to convert HTML to MHTML programmatically, please see the following C# code examples.

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!

1using System.IO;
2using Aspose.Html.Converters;
3using Aspose.Html.Saving;
4...
5    // Invoke the ConvertHTML() method to convert the HTML code to MHTML
6    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 document as an MHTML file. You need to pass the HTMLDocument, MHTMLSaveOptions, and output file path to the ConvertHTML() method to convert HTML to MHTML.
 1using System.IO;
 2using Aspose.Html;
 3using Aspose.Html.Converters;
 4using Aspose.Html.Saving;
 5...
 6    // Prepare a path to a source HTML file
 7    string documentPath = Path.Combine(DataDir, "drawing.html");
 8
 9    // Prepare a path for converted file saving 
10    string savePath = Path.Combine(OutputDir, "drawing-output.mht");
11
12    // Initialize an HTML document from the file
13    using var document = new HTMLDocument(documentPath);
14
15    // Initialize MHTMLSaveOptions 
16    var options = new MHTMLSaveOptions();
17
18    // Convert HTML to MHTML
19    Converter.ConvertHTML(document, options, savePath);

You can download the complete examples and data files from GitHub.

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.

To learn more about MHTMLSaveOptions, please read the Fine-Tuning Converters article.

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:

 1using System.IO;
 2using Aspose.Html;
 3using Aspose.Html.Converters;
 4using Aspose.Html.Saving;
 5...
 6     // Prepare HTML code with a link to another file and save it to the file as 'document.html'
 7     var code = "<span>Hello, World!!</span> " +
 8                "<a href='document2.html'>click</a>";
 9     File.WriteAllText("document.html", code);
10
11     // Prepare HTML code and save it to the file as 'document2.html'
12     code = @"<span>Hello, World!!</span>";
13     File.WriteAllText("document2.html", code);
14
15     string savePath = Path.Combine(OutputDir, "output-options.mht");
16
17     // Change the value of the resource linking depth to 1 in order to convert document with directly linked resources
18     var options = new MHTMLSaveOptions()
19     {
20         ResourceHandlingOptions =
21         {
22             MaxHandlingDepth = 1
23         }
24     };
25
26     // Convert HTML to MHTML
27     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.

You can download the complete examples and data files from GitHub.

Aspose.HTML offers a free online HTML to MHTML Converter that converts HTML to MHTML with high quality, easy and fast. Just upload, convert your files and get results in a few seconds!

Text “Banner HTML to MHTML Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.