Convert HTML to Other Formats

Convert HTML to EML

Aspose.Email for Java provides a way to convert HTML files to EML format using the MailMessage.load and MailMessage.save methods to load the existing HTML file and save it in EML format respectively:

MailMessage eml = MailMessage.load("myContent.html", new HtmlLoadOptions());
eml.save("output.eml", SaveOptions.getDefaultEml());

In the code sample, the HtmlLoadOptions class allows you to specify additional options when loading a MailMessage from HTML format. The following code sample demonstrates the use of this class. In the example, it requests a plain text representation of the message body:

// Create an instance of HtmlLoadOptions
HtmlLoadOptions loadOptions = new HtmlLoadOptions();

// Request a plain text view to be generated along with the HTML one
loadOptions.shouldAddPlainTextView(true);

// Load an HTML file
MailMessage mailMessage = MailMessage.load("input.html", loadOptions);

// Access the plain text view of the email message
String plainTextView = mailMessage.getAlternateViewContent("text/plain");

// Print or further process the plain text view
System.out.println("Plain Text View:");
System.out.println(plainTextView);

Convert HTML to EMLX

You can easily convert HTML files to EMLX. All the options and classes provided by the API for HTML to EML conversion are available for this type of conversion as well:

MailMessage eml = MailMessage.load("myContent.html", new HtmlLoadOptions());
eml.save("output.emlx", SaveOptions.getDefaultEmlx());

For additional settings, see the Convert HTML to EML paragraph.

Convert HTML to ICS

To perform the task, the library offers the Appointment class to represent and manage calendar events. The following code sample demonstrates how to create an appointment from HTML content and save it in the ICS (iCalendar) file format:

// Read the HTML content
String htmlContent = new String(Files.readAllBytes(Paths.get("content.html")), StandardCharsets.UTF_8);

MailAddressCollection attendees = new MailAddressCollection();
attendees.add("attendees@domain.com");

// Create and initialize an instance of the Appointment class
Appointment appointment = new Appointment(
        "Meeting Room 3 at Office Headquarters",             // Location
        "Monthly Meeting",                                   // Summary
        "Please confirm your availability.",                 // Description
        new GregorianCalendar(2015, java.util.Calendar.FEBRUARY, 8, 13, 0, 0).getTime(), // Start date
        new GregorianCalendar(2015, java.util.Calendar.FEBRUARY, 8, 14, 0, 0).getTime(), // End date
        new MailAddress("from@domain.com"),                  // Organizer
        attendees);

appointment.setHtmlDescription(htmlContent);

// Save the event to an ICS file
appointment.save("output.ics", AppointmentSaveFormat.Ics);

Generate MBOX from HTML content

To perform HTML to MBOX conversion, use the load method of the MailMessage class, specifying the HTML content file path and an instance of HtmlLoadOptions. This method parses the HTML content and generates a corresponding MailMessage object, preserving the structure and formatting of the original HTML. After loading the HTML content into a MailMessage object, write the message to an MBOX file using the MboxrdStorageWriter class:

try (MailMessage eml = MailMessage.load("content.html", new HtmlLoadOptions());
     MboxrdStorageWriter writer = new MboxrdStorageWriter("output.mbox", new MboxSaveOptions())) {
    writer.writeMessage(eml);
}

Convert HTML to MHTML

Use the load method of the MailMessage class to load the existing file, specifying a path to it and an instance of HtmlLoadOptions. After loading the HTML content into a MailMessage object, developers can save it as an MHTML file using the save method, specifying the desired output file path and utilizing the SaveOptions.getDefaultMhtml() option:

MailMessage eml = MailMessage.load("content.html", new HtmlLoadOptions());
eml.save("output.mhtml", SaveOptions.getDefaultMhtml());

The MhtSaveOptions class provides a variety of options for configuring the behavior and settings of the output MHTML file instead of SaveOptions.getDefaultMhtml(). The most popular of them are:

  • MhtFormatOptions - Allows you to customize how the email message is saved in MHT format to best suit your needs.
  • SaveAttachments - Gets or sets a value indicating whether to save attachments.
  • SaveAllHeaders - Defines whether all headers should be saved in the output MHTML or not. The default value is false.
  • PreserveOriginalDate - Defines whether the original date should be kept in the mail message when saving or not. The default value is true.

The following code sample demonstrates how these properties can be used:

MhtSaveOptions mhtSaveOptions = new MhtSaveOptions();
mhtSaveOptions.setMhtFormatOptions(MhtFormatOptions.WriteHeader);
mhtSaveOptions.setSaveAttachments(true);
mhtSaveOptions.setSaveAllHeaders(true);
mhtSaveOptions.setPreserveOriginalDate(true);

MailMessage eml = MailMessage.load("content.html", new HtmlLoadOptions());
eml.save("output.mhtml", mhtSaveOptions);

Convert HTML to MSG

After loading the HTML content into a MailMessage object, save it as an MSG file using the save method, specifying the desired output file path and utilizing the SaveOptions.getDefaultMsgUnicode() option. This option ensures that the output file is saved in the MSG format with Unicode encoding.

MailMessage eml = MailMessage.load("content.html", new HtmlLoadOptions());
eml.save("output.msg", SaveOptions.getDefaultMsgUnicode());

Convert HTML to OFT

After loading the HTML content into a MailMessage object, save it as an OFT file using the save method, specifying the desired output file path and utilizing the SaveOptions.getDefaultOft() option:

MailMessage eml = MailMessage.load("content.html", new HtmlLoadOptions());
eml.save("template.oft", SaveOptions.getDefaultOft());

Add message with source HTML content to PST

HTML to PST conversion involves creating a new PST (Personal Storage Table) file with a new folder, loading an HTML file and adding it to the new folder:

  1. Create an instance of a PersonalStorage object representing a new PST file using the create method of the PersonalStorage class.
  2. Access the root folder of the PST file and add a subfolder to it using the addSubFolder method of the FolderInfo class.
  3. Load the content of an HTML file into a MapiMessage object using the load method with an instance of HtmlLoadOptions to specify that the content is in HTML format.
  4. Add the loaded MapiMessage object (representing the HTML content) to the folder within the PST file using the addMessage method.
try (PersonalStorage pst = PersonalStorage.create("outputFile.pst", FileFormatVersion.Unicode)) {
    FolderInfo inbox = pst.getRootFolder().addSubFolder("Inbox");
    MapiMessage msg = MapiMessage.load("content.html", new HtmlLoadOptions());
    inbox.addMessage(msg);
}

Add message with source HTML content to OST

The following code sample with steps will show you how these components work together to add HTML content to an OST file:

  1. Load an existing OST file with the fromFile method of the PersonalStorage class. Pass true as the second argument to open the storage for writing.
  2. Load the HTML file using the load method of the MapiMessage class representing an email message in Microsoft Outlook format.
  3. Specify HtmlLoadOptions to enable additional options when loading a message from HTML format.
  4. Retrieve the root folder of the OST file using the getRootFolder() method of the PersonalStorage object.
  5. Obtain the Inbox folder within the OST file using the getSubFolder method on the root folder.
  6. Add the loaded MapiMessage object (representing the HTML content) to the Inbox folder using the addMessage method on the folder.
try (PersonalStorage ost = PersonalStorage.fromFile("storage.ost", true)) {
    MapiMessage msg = MapiMessage.load("content.html", new HtmlLoadOptions());
    FolderInfo folderInfo = ost.getRootFolder().getSubFolder("Inbox");
    folderInfo.addMessage(msg);
}

Convert HTML to VCF

The following code sample demonstrates how to create a contact item, populate it with HTML content, and save it to a VCF file:

  1. Read the content of an HTML file into a string variable.
  2. Create a new MapiContact object by instantiating the MapiContact class.
  3. Set the contact properties: display name, email address.
  4. Set the body content of the contact to HTML using setBodyContent.
  5. Save the contact as a VCF file using the save method.
String content = new String(Files.readAllBytes(Paths.get("content.html")), StandardCharsets.UTF_8);

// Create a new MapiContact
MapiContact contact = new MapiContact();
contact.getNameInfo().setDisplayName("John Doe");
contact.getElectronicAddresses().getEmail1().setEmailAddress("john.doe@example.com");
contact.setBodyContent(content, BodyContentType.Html);

// Save the contact to a VCF file
contact.save("contact.vcf", ContactSaveFormat.VCard);