Convert HTML to MHTML in C#

To convert HTML to MHTML in C#, use Converter.ConvertHTML() with an HTML source, MHTMLSaveOptions, and an output path. Aspose.HTML for .NET can package HTML and related resources into a single MHTML web archive, and MHTMLSaveOptions lets you control page URL restrictions, resource URL restrictions, and handling depth.

This guide shows the core HTML to MHTML workflows for C# applications: a one-line conversion, a file-based conversion, and custom MHTML save options. Before running the examples, install and configure Aspose.HTML for .NET in your project. The complete C# example project is available in the Aspose.HTML for .NET GitHub repository.

What Is MHTML?

MHTML, commonly called MIME HTML, is a MIME-based web archive format that combines HTML markup and related resources in one file. MIME stands for Multipurpose Internet Mail Extensions. MHTML archives commonly use the .mhtml or .mht file extension.

RFC 2557, MIME Encapsulation of Aggregate Documents, such as HTML (MHTML), defines a multipart/related structure containing a root HTML resource and related resources such as images and stylesheets. Although the format was initially designed to transfer complete HTML documents by email, it is also used to store and archive web pages.

HTML vs. MHTML

FeatureHTMLMHTML
File organizationAn HTML markup documentA MIME archive with a root HTML document and related resources
Related resourcesUsually referenced as separate files or URLs, although some resources can be embedded in the markupPackaged as separate MIME parts within the archive when they are retrieved and included
Common extensions.html, .htm.mhtml, .mht
Typical useWebsites, web applications, and editable web contentSingle-file archiving, offline sharing, and email transfer
Offline useMay require local files or network access for referenced resourcesIncluded resources remain available from the archive without separate files

Use HTML to MHTML conversion when your application needs a portable single-file snapshot of a web page or generated HTML content.

Convert HTML to MHTML with One Line of C#

For simple input, the static Converter class provides a short path from HTML content to an MHTML file. The example below passes HTML code, a base URI, default MHTMLSaveOptions, and the output file path to ConvertHTML().

1// Convert HTML to MHTML using C#
2
3// Invoke the ConvertHTML() method to convert HTML to MHTML
4Converter.ConvertHTML(@"<h1>Hellow, Word!</h1>", ".", new MHTMLSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.mht"));

Use this pattern when your HTML is already available as a string and default MHTML packaging settings are enough. For file input or custom resource handling, use the workflows below.

Convert an HTML File to MHTML in C#

A typical file-based conversion uses an HTMLDocument instance, an MHTMLSaveOptions object, and Converter.ConvertHTML(). You can load HTML from a local file, HTML code, stream, or URL. For more loading patterns, see Creating an HTML Document.

The workflow is:

  1. Load the HTML file with an HTMLDocument constructor.
  2. Create an MHTMLSaveOptions instance.
  3. Call Converter.ConvertHTML(document, options, savePath) to write the MHTML archive.

The following C# code converts an HTML document to MHTML.

 1// Convert HTML to MHTML in C#
 2
 3// Prepare a path to a source HTML file
 4string documentPath = Path.Combine(DataDir, "drawing.html");
 5
 6// Prepare a path to save the converted file
 7string savePath = Path.Combine(OutputDir, "drawing-output.mht");
 8
 9// Initialize an HTML document from the file
10using HTMLDocument document = new HTMLDocument(documentPath);
11
12// Initialize MHTMLSaveOptions 
13MHTMLSaveOptions options = new MHTMLSaveOptions();
14
15// Convert HTML to MHTML
16Converter.ConvertHTML(document, options, savePath);

Short HTML to MHTML Recipes

Customize HTML to MHTML with MHTMLSaveOptions

Use MHTMLSaveOptions when the output archive needs custom resource handling. The options object is passed to ConvertHTML() and controls which pages and resources are included in the generated MHTML file.

OptionUse it to control
PageUrlRestrictionRestrictions applied to URLs of handled pages. The default value is RootAndSubFolders.
ResourceUrlRestrictionRestrictions applied to handled resources such as CSS, JavaScript, images, and other linked files. The default value is SameHost.
MaxHandlingDepthMaximum depth of pages that will be handled during packaging. The default value is 0.

The following example creates an MHTML file with custom save options.

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

In this example, MHTMLSaveOptions is passed to Converter.ConvertHTML() together with the loaded HTML document and output path. The sample uses MaxHandlingDepth = 1, which means only pages directly referenced from the saved document will be handled.

For deeper resource and rendering settings, see Fine-Tuning Converters.

Related MHTML Workflows

Related HTML Conversion Guides

Other Platforms

Try Online HTML to MHTML Conversion

                
            

Use the online HTML to MHTML Converter for quick manual conversion. Use Aspose.HTML for .NET when HTML to MHTML packaging must run inside a C# application, service, or archive workflow.

HTML to MHTML Converter

FAQ

Can I convert HTML to MHTML for free?

You can evaluate Aspose.HTML for .NET before purchasing a license, but converted documents created in evaluation mode contain watermarks and are limited to four pages. For unrestricted testing, request a free 30-day Temporary License. For occasional manual conversions, use the free online HTML to MHTML Converter.

See Licensing Aspose.HTML for .NET for evaluation limitations and license setup.

Does the MHTML file include all external page resources?

MHTML can package the HTML and retrieved resources into one archive, but a resource must be accessible when the document is saved. Images, stylesheets, or fonts can be missing if their URLs are invalid, blocked, protected by authentication, or excluded by resource-handling settings.