Programmatic HTML to MHTML Conversion for .NET
Aspose.HTML for .NET packages HTML documents and accessible resources as MHTML web archives inside C# applications. Load a file, URL, string, stream, or HTMLDocument , create MHTMLSaveOptions , and use the corresponding save workflow.
MHTML is useful when a workflow needs one archive instead of a separate HTML file, stylesheets, and images.
C# Code Example and Interactive HTML to MHTML Converter
Use the interactive example below to convert an HTML file or URL to MHTML and review the corresponding C# code. Use the generated code as a starting point, then add the settings required by your application.
using Aspose.Html;
using Aspose.Html.Converters;
using Aspose.Html.Saving;
using HTMLDocument document = new HTMLDocument("{{input lower}}");
{{#if_output 'PDF'}}
PdfSaveOptions options = new PdfSaveOptions();
{{/if_output}}
{{#if_output 'MHTML'}}
MHTMLSaveOptions options = new MHTMLSaveOptions();
{{/if_output}}
{{#if_output 'DOCX'}}
DocSaveOptions options = new DocSaveOptions();
{{/if_output}}
{{#if_output 'XPS'}}
XpsSaveOptions options = new XpsSaveOptions();
{{/if_output}}
{{#if_output 'MD'}}
MarkdownSaveOptions options = new MarkdownSaveOptions();
{{/if_output}}
{{#if_output 'BMP' 'JPG' 'GIF' 'PNG' 'TIFF'}}
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.{{output param2 camel}});
{{/if_output}}
{{#if_output 'BMP' 'JPG' 'GIF' 'PNG' 'TIFF' 'PDF' 'MHTML' 'MD' 'XPS' 'DOCX'}}
Converter.ConvertHTML(document, options, "output.{{output lower}}");
{{/if_output}}
{{#if_output 'XHTML'}}
document.Save("output.xhtml", new HTMLSaveOptions() { DocumentType = HTMLSaveOptions.XHTML });
{{/if_output}}
How to Convert HTML to MHTML in C#
- Load the source with an HTMLDocument constructor.
- Create
MHTMLSaveOptionsand configure the required output settings. - Call Converter.ConvertHTML() with the document, options, and output destination.
- Validate the generated MHTML with representative source documents.
HTML to MHTML Conversion Options and Documentation
Read the
HTML to MHTML documentation
and consult the MHTMLSaveOptions reference for available settings.
Use
MHTMLSaveOptions
when packaging an HTML document and its resources as a MIME web archive. Save to a file path or use
ICreateStreamProvider
when the application requires custom output streams.
HTML to MHTML Conversion Notes
- Availability determines archive contents. Only resources accessible during conversion can be included.
- Relative URLs need context. Provide a valid base URI when the source is a string or stream.
- Dynamic resources need testing. Authentication, blocked requests, or client-side loading can leave assets outside the archive.
- Viewer support varies. Test the resulting MHTML in the applications used by the target workflow.
- Repeatability requires controlled inputs. Stable resources produce more predictable archives.
Install Aspose.HTML for .NET
Add the package to your project, then use the C# example above as the minimal HTML to MHTML workflow.
Install Aspose.HTML for .NET from NuGet with the .NET CLI:
dotnet add package Aspose.HTML
You can also use the NuGet Package Manager UI or run Install-Package Aspose.HTML in the Package Manager Console. See the
installation guide
for setup details, or request a
temporary license
to evaluate the API without evaluation limitations.
HTML to MHTML Conversion FAQ
1. How do I convert HTML to MHTML in C#?
Load the source into an HTMLDocument, create MHTMLSaveOptions, and call Converter.ConvertHTML() with the document and output path. The result is a MIME web archive containing HTML and resources available for packaging.
2. Can I archive a webpage URL as MHTML?
Yes. Load the URL with HTMLDocument and ensure its resources are accessible during conversion.
3. Does MHTML include images and stylesheets?
It can package resources resolved by the document. Missing, blocked, or dynamically loaded assets may not be included.
4. Why are resources missing from the archive?
Check base URIs, network access, authentication, and the availability of every referenced resource.
5. Can I write MHTML to a stream?
Yes. Use a Converter.ConvertHTML() overload that accepts an
ICreateStreamProvider
. The provider lets the application write the MHTML archive to memory or another managed destination instead of a file path.
Other Supported HTML Conversions
Explore other HTML conversion APIs and C# examples: