Convert OFT Files to Various Formats in Java
One of the essential features of Aspose.Email for Java is the ability to convert OFT (Outlook File Template) files to various other formats, such as EML, EMLX, MSG, MHTML, and more. In this section, we will guide you through the process of converting OFT files to these formats.
When converting OFT files to other formats using Aspose.Email for Java, several key components are involved in the process:
- MapiMessage class - represents a Microsoft Outlook message in memory. It is used to load and manipulate OFT files.
loadmethod - loads an OFT file into a MapiMessage object. It is a static method of the MapiMessage class that reads an OFT file from the specified path.savemethod - saves the MapiMessage object in a specified format, such as EML, MSG, or MHTML, to a target file path.- SaveOptions class - provides options for saving the message in different formats. It contains predefined options for various formats like EML, MSG, and MHTML and ensures that the message is saved with the correct format settings.
Convert OFT to EML
Converting OFT files to EML format is often essential for email data migration, archiving, or ensuring compatibility with various email clients. The EML format is widely supported and can be opened by numerous email applications:
- Load the OFT file into a MapiMessage object using MapiMessage.load.
- Save the loaded file using the
savemethod, specifying the target file name and the saving options.
MapiMessage oft = MapiMessage.load("template.oft");
oft.save("message.eml", SaveOptions.getDefaultEml());
Aspose.Email for Java offers several customization options when converting OFT files to EML format.
Modify Email Subject or Body
You can modify the email’s subject or body before saving it as EML.
// Load the OFT file
MapiMessage oft = MapiMessage.load("template.oft");
// Modify the email subject
oft.setSubject("Updated Subject");
// Modify the email body
oft.setBody("This is the updated body content of the email.");
// Save the modified email as EML
oft.save("updated_message.eml", SaveOptions.getDefaultEml());
Handling Attachments
You can add or remove attachments from the email before conversion.
// Load the OFT file
MapiMessage oft = MapiMessage.load("template.oft");
// Add a new attachment
oft.getAttachments().add("new_attachment.txt", Files.readAllBytes(Paths.get("new_attachment.txt")));
// Remove an existing attachment (if any)
if (oft.getAttachments().size() > 0) {
oft.getAttachments().removeAt(0);
}
// Save the modified email as EML
oft.save("message_with_attachments.eml", SaveOptions.getDefaultEml());
The range of options also includes setting encoding, managing headers, and more — all of which help tailor the conversion process to meet specific requirements.
Convert OFT to EMLX
- Use the MapiMessage.load method to load the OFT file into a MapiMessage object.
- Once the OFT file is loaded, use the
savemethod of the MapiMessage object to save it in the EMLX format.
MapiMessage oft = MapiMessage.load("template.oft");
oft.save("message.emlx", SaveOptions.getDefaultEmlx());
Customize Message Properties
Aspose.Email provides various customization options while converting OFT files to EMLX format. You can modify properties such as subject, body, attachments, etc.
// Load the OFT file
MapiMessage mapiMessage = MapiMessage.load("input.oft");
// Customize message properties
mapiMessage.setSubject("Customized Subject");
mapiMessage.setBody("This is the customized body of the email.");
// Add an attachment
mapiMessage.getAttachments().add("attachment.txt", "Attachment content".getBytes(StandardCharsets.UTF_8));
// Convert and save the customized message in EMLX format
MailMessage emlMessage = mapiMessage.toMailMessage(new MailConversionOptions());
emlMessage.save("output.emlx", SaveOptions.getDefaultEmlx());
Convert OFT to HTML
Converting OFT files to HTML format allows the email content to be easily displayed in web browsers and integrated into web applications. Aspose.Email for Java simplifies this process, ensuring that the converted HTML maintains the original formatting and structure of the OFT file.
- Use the MapiMessage.load method to load an OFT file into a MapiMessage object.
- Save the loaded message as an HTML file using the
savemethod and choosingSaveOptions.getDefaultHtml()as the save options.
MapiMessage oft = MapiMessage.load("template.oft");
oft.save("message.html", SaveOptions.getDefaultHtml());
Aspose.Email for Java offers various customization options while converting OFT files to HTML format, allowing developers to tailor the output to specific requirements.
Embedding Resources
To ensure that images and other resources are embedded within the HTML file as base64-encoded data, use the HtmlSaveOptions class and set the resource rendering mode to EmbedIntoHtml.
MapiMessage oft = MapiMessage.load("template.oft");
// Create HTML save options with embedded resources
HtmlSaveOptions options = SaveOptions.getDefaultHtml();
options.setResourceRenderingMode(ResourceRenderingMode.EmbedIntoHtml);
// Save as HTML with embedded resources
oft.save("message_embedded.html", options);
ResourceRenderingMode replaces the older EmbedResources switch. Besides EmbedIntoHtml, it offers SaveToFile (write every resource next to the HTML file), SubstituteFromFile and None.
Specifying Character Encoding
The character encoding of the output is taken from the message itself. Convert the template to a MailMessage, set the preferred text encoding, and then save it:
MapiMessage oft = MapiMessage.load("template.oft");
MailMessage eml = oft.toMailMessage(new MailConversionOptions());
eml.setPreferredTextEncoding(StandardCharsets.UTF_8);
// Save as HTML with the specified encoding
eml.save("message_utf8.html", SaveOptions.getDefaultHtml());
Inline Images
Inline images are written into the HTML output as base64-encoded data: URIs when the resource rendering mode is set to EmbedIntoHtml. If you prefer separate image files referenced by relative paths, use SaveToFile together with setUseRelativePathToResources(true):
MapiMessage oft = MapiMessage.load("template.oft");
HtmlSaveOptions options = new HtmlSaveOptions();
options.setResourceRenderingMode(ResourceRenderingMode.SaveToFile);
options.setUseRelativePathToResources(true);
oft.save("message_with_images.html", options);
Convert OFT to MSG
An Outlook template differs from a message only by its message class and flags, so saving it as an ordinary MSG file takes a single call:
MapiMessage oft = MapiMessage.load("template.oft");
oft.save("message.msg", SaveOptions.getDefaultMsgUnicode());
Convert OFT to MHTML
MapiMessage oft = MapiMessage.load("template.oft");
oft.save("message.mhtml", SaveOptions.getDefaultMhtml());