HTML to MHT Java

MHT files combine HTML markup, images, CSS, and related resources into a single, portable archive. Converting an existing HTML page to MHT (or MHTML) is useful for offline viewing, email embedding, or archiving. This article explains step‑by‑step how to convert HTML to MHT or MHTML programmatically in Java using Aspose.HTML.

HTML to MHT File Converter – Java API Installation

Aspose.HTML for Java provides a robust API for handling HTML, MHTML, XPS, and many other formats. Add the library to your project via Maven, Gradle, or by downloading the JAR directly.

Maven repository configuration

 <repositories>
     <repository>
         <id>aspose-repo</id>
         <name>Aspose Repository</name>
         <url>http://repository.aspose.com/repo/</url>
     </repository>
 </repositories>

Maven dependency

 <dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-html</artifactId>
        <version>21.12</version>
        <classifier>jdk17</classifier>
    </dependency>
 </dependencies>

Tip: If you use Gradle, replace the <repositories> and <dependencies> blocks with the equivalent repositories { maven { url "http://repository.aspose.com/repo/" } } and implementation "com.aspose:aspose-html:21.12:jdk17" statements.

Convert HTML to MHT or MHTML in Java

Follow these three simple steps to transform an HTML document into an MHT or MHTML archive:

  1. Create a MHTMLSaveOptions instance – this object defines the output format and optional settings.
  2. Load the source HTML – use HTMLDocument to read a local file, URL, or HTML string.
  3. Invoke Converter.convertHTML – pass the document, the save options, and the target file path.

The sample below demonstrates a complete conversion from an HTML file to an MHT archive:

The same code works for MHTML; simply change the file extension of the output path to .mhtml.

Convert HTML to MHT with Advanced Options in Java

Aspose.HTML’s MHTMLSaveOptions class exposes several properties that let you fine‑tune the resulting archive, such as resource embedding, encoding, and CSS handling. Use the following workflow for a customized conversion:

  1. Instantiate MHTMLSaveOptions and set advanced properties (e.g., setEmbedImages(true), setEncoding("UTF-8")).
  2. Configure additional options like setCssStyleSheetExportMode to control how CSS is stored.
  3. Call Converter.convertHTML with the configured options to generate the final MHT file.

Here is a code example that applies common advanced settings:

Explore Aspose.HTML for Java API

Visit the official API documentation to discover more features, such as PDF conversion, image extraction, and DOM manipulation.

Get Free License

Evaluate the full capabilities of Aspose.HTML for Java by requesting a free temporary license.

Conclusion

You now have a clear, production‑ready approach for converting HTML pages to MHT or MHTML archives in Java. Whether you need a simple one‑line conversion or advanced control over embedded resources, Aspose.HTML provides the tools you need. For further assistance, join the discussion on the forum.

See Also

Convert EPUB to XPS in Java