Convert MSG Files to Various Formats in Java
MSG is a file format used by Microsoft Outlook and Exchange to store individual email messages. An MSG file contains the email’s data, including headers, body, attachments, and metadata. These files are typically created when an email is saved to disk from an Outlook email client. However, there are scenarios where you may need to convert MSG files to other formats for compatibility, archival, or sharing purposes.
The primary components involved in these conversion processes include classes, methods, and enumerations that facilitate the loading, manipulation, and saving of MSG files:
- MapiMessage class - represents an MSG file, providing properties and methods specific to the MSG format. It is used to load and manipulate MSG files before conversion.
- MailMessage class - represents an email message. It is used to hold the email content once it is converted from the MSG format, and subsequently save it in the desired format.
- SaveOptions - defines the saving options and target formats for the MailMessage object. Examples include
SaveOptions.getDefaultEml(),SaveOptions.getDefaultHtml(),SaveOptions.getDefaultMhtml(), etc.
Methods
MapiMessage.load(String fileName)- loads an MSG file from the specified file path into a MapiMessage object.MapiMessage.toMailMessage(MailConversionOptions options)- converts the MapiMessage object into a MailMessage object for further processing and saving.MailMessage.save(String fileName, SaveOptions options)- saves the MailMessage object to the specified file path in the desired format.
Convert MSG to EML
EML files are widely supported across different email clients, making it easier to share and access emails on various platforms. The code sample below demonstrates how to load an MSG file and save it as an EML file:
- Load the MSG file using the MailMessage.load method.
- Save the loaded message as an EML file using the
savemethod of the MailMessage class.
// Load an existing MSG file
MailMessage message = MailMessage.load("myMessage.msg");
message.save("output.eml", SaveOptions.getDefaultEml());
You can customize email headers before saving the email in EML format. This is useful for adding metadata or additional information. Use the EmlSaveOptions class to specify additional settings when saving the email. For instance, you can control how the email body is saved, manage encoding, and specify whether the embedded message format should be preserved.
Convert MSG to EMLX
Converting MSG files to EMLX format is essential for scenarios where email messages need to be compatible with Apple Mail and other email clients that support the EMLX format.
- Load the MSG file with the MailMessage.load method.
- Use the
savemethod of the MailMessage class to save the MSG file in EMLX format.
// Load the MSG file to be converted
MailMessage message = MailMessage.load("My File.msg");
// Save the MSG as an EMLX
message.save("Saved File.emlx", SaveOptions.createSaveOptions(MailMessageSaveType.getEmlxFormat()));
Aspose.Email for Java offers several features that can be applied in the process of converting MSG to EMLX. These features enhance the conversion process by providing additional control, customization, and handling of various aspects of email messages. The code sample below demonstrates the implementation of some of them:
// Load the MSG file to be converted
MailMessage message = MailMessage.load("My File.msg");
// Preserve the original format of embedded messages
EmlSaveOptions saveOptions = (EmlSaveOptions) SaveOptions.createSaveOptions(MailMessageSaveType.getEmlxFormat());
saveOptions.setPreserveEmbeddedMessageFormat(true);
// Handle attachments
if (message.getAttachments().size() > 0) {
System.out.println("The email contains attachments which will be preserved.");
}
// Customize email headers
message.getHeaders().add("X-Custom-Header", "CustomHeaderValue");
// Save the MSG file as EMLX with the specified options
message.save("Saved File.emlx", saveOptions);
Convert MSG to HTML
Converting MSG files to HTML format can be valuable for displaying emails in web applications or for archiving purposes:
- Load the MSG file with the MailMessage.load method. The format is detected automatically.
- Save the loaded message in HTML format using the
savemethod. TheSaveOptions.getDefaultHtml()parameter ensures that the file is saved correctly in HTML format.
// Load an existing MSG file
MailMessage message = MailMessage.load("myMessage.msg");
message.save("output.html", SaveOptions.getDefaultHtml());
Advanced MSG to HTML Conversion Features
- Preserving Email Formatting: Aspose.Email ensures that the original formatting of the email, including fonts, styles, and layout, is preserved during the conversion to HTML.
- Handling Attachments: The conversion process can include attachments, either embedding them within the HTML or linking to them externally. See Convert EML to HTML for the available resource rendering modes.
- Customizing HTML Output: You can customize the HTML output by modifying the document structure, adding custom styles, or including additional HTML elements.
- Converting Inline Images and Embedded Objects: Inline images and embedded objects are converted and preserved within the HTML content.
- Encoding Options: You can specify encoding options to ensure that the HTML output is compatible with various browsers and email clients.
Convert MSG to ICS
Converting MSG files containing calendar appointments to ICS format is a common task for ensuring compatibility with various calendar applications:
- Initialize the MapiMessage object and load the MSG file using the
loadmethod. - Check whether the loaded MSG file is of the Calendar type using MapiItemType. This is crucial to ensure that the file contains a calendar appointment before attempting the conversion.
- Convert the MapiMessage object into a MapiCalendar object, which is necessary for saving the appointment in ICS format.
- Save the calendar item in ICS format with the
savemethod.
MapiMessage msg = MapiMessage.load("appointment.msg");
if (msg.getSupportedType() == MapiItemType.Calendar) {
MapiCalendar calendar = (MapiCalendar) msg.toMapiMessageItem();
calendar.save("appointment.ics", AppointmentSaveFormat.Ics);
}
Advanced MSG to ICS Conversion Features
- Preserving Appointment Details: date, time, location, attendees, and recurrence patterns.
- Handling Recurring Appointments: all recurrence patterns are accurately converted and reflected in the ICS file.
- Customizing Appointment Properties: you can customize various properties of the appointment, such as setting reminders, adding categories, and modifying custom fields before saving as ICS.
- Managing Attachments: all relevant documents and files are included within the ICS file.
- Setting Time Zones: set and adjust time zones for the appointment to make time-related data accurate and consistent across different regions.
Convert MSG to MBOX
Converting MSG files to MBOX format can be essential for managing and archiving email messages across different platforms. Aspose.Email for Java provides the writeMessage method of the MboxrdStorageWriter class to write a message to an MBOX file:
- Load the MSG file using MailMessage.load, specifying MsgLoadOptions.
- Create an MboxrdStorageWriter object to write the messages to an MBOX file named “output.mbox”.
- Write the loaded message to the MBOX file using the
writeMessagemethod.
try (MailMessage message = MailMessage.load("inputFile.msg", new MsgLoadOptions());
MboxrdStorageWriter writer = new MboxrdStorageWriter("output.mbox", new MboxSaveOptions())) {
writer.writeMessage(message);
}
Special Features for MSG to MBOX Conversion
The features provided by Aspose.Email for Java to enhance the process of converting MSG files to MBOX format include:
- Preserving email properties
- Attachments handling
- Email headers customization
- Multiple MSG files conversion
- Setting email save options
Convert MSG to MHT/MHTML
Converting MSG files to MHT format is useful for creating a web archive of email messages, which can be easily shared or stored:
- Use the MapiMessage.load method to load the MSG file.
- Use the
savemethod with the appropriate save options to save the message in MHT format.
MapiMessage msg = MapiMessage.load("file.msg");
msg.save("file.mht", SaveOptions.getDefaultMhtml());
Aspose.Email for Java provides an array of special features that enhance the conversion of MSG files to MHT format. It ensures the preservation of the email’s original formatting and allows for the inclusion of attachments within the MHT file. Customization options through MhtSaveOptions enable embedded resources, management of inline images, and the addition of custom headers. Furthermore, it handles embedded objects and offers detailed control over encoding for a flawless conversion experience.
Convert MSG to OFT
Converting MSG files to OFT (Outlook Template File) can be essential for creating reusable email templates in Microsoft Outlook. By utilizing the MapiMessage class, you can convert an MSG file to the OFT format:
- Create a new MapiMessage object with the sender’s email, recipient’s email, subject, and body of the email template — or load an existing MSG file.
- Save the MapiMessage with MsgSaveOptions constructed with
MailMessageSaveType.getOutlookTemplateFormat().
try (MapiMessage mapi = new MapiMessage("test@from.to", "test@to.to", "template subject", "Template body")) {
mapi.save("mapiToOft.oft", new MsgSaveOptions(MailMessageSaveType.getOutlookTemplateFormat()));
}
Convert MSG to PDF
Aspose.Email for Java deals with network protocols and Microsoft Outlook features and cannot, on its own, render an email as a fixed-layout document. To convert an MSG file to PDF, first use Aspose.Email to convert the message to MHTML format, and then use Aspose.Words for Java to load the MHTML and save it as PDF.
An email message can contain attachments of different media types. Since each attachment can be of a different media type, Aspose.Email ignores these attachments while converting to MHTML; that is, only inline images in a message become part of the MHTML, and any regular attachments are ignored.
- Load the email message using the MailMessage class.
- Save the email message to a stream as MHTML.
- Load the stream using Aspose.Words, specifying the MHTML load format.
- Save the message as PDF.
MailMessage mailMsg = MailMessage.load("message.msg", new MsgLoadOptions());
// Save the message to a stream in MHTML format
ByteArrayOutputStream mhtmlStream = new ByteArrayOutputStream();
mailMsg.save(mhtmlStream, SaveOptions.getDefaultMhtml());
// Load the MHTML stream with Aspose.Words for Java
com.aspose.words.LoadOptions loadOptions = new com.aspose.words.LoadOptions();
loadOptions.setLoadFormat(com.aspose.words.LoadFormat.MHTML);
com.aspose.words.Document document =
new com.aspose.words.Document(new ByteArrayInputStream(mhtmlStream.toByteArray()), loadOptions);
// Save the document as PDF
document.save("SaveEmailAsPDF_out.pdf", com.aspose.words.SaveFormat.PDF);
See also Saving a MSG as PDF.
Convert MSG to TIFF
The same MHTML bridge produces a TIFF image. If the message spans multiple pages, a multi-page TIFF file is generated.
- Read the MSG file with the MailMessage class.
- Save it to a stream in MHTML format.
- Load the MHTML stream into the Aspose.Words
Documentclass. - Call the
savemethod to save the document as TIFF.
// Load the MSG file using Aspose.Email for Java
MailMessage msg = MailMessage.load("message.msg", new MsgLoadOptions());
// Convert the MSG to MHTML and save it to a stream
ByteArrayOutputStream msgStream = new ByteArrayOutputStream();
msg.save(msgStream, SaveOptions.getDefaultMhtml());
// Load the MHTML stream using Aspose.Words for Java and save the document as a TIFF image
com.aspose.words.LoadOptions loadOptions = new com.aspose.words.LoadOptions();
loadOptions.setLoadFormat(com.aspose.words.LoadFormat.MHTML);
com.aspose.words.Document msgDocument =
new com.aspose.words.Document(new ByteArrayInputStream(msgStream.toByteArray()), loadOptions);
msgDocument.save("Outlook-Aspose_out.tif", com.aspose.words.SaveFormat.TIFF);
aspose-words artifact in addition to aspose-email. The fully qualified names are used deliberately, because LoadOptions and SaveFormat exist in both libraries.