Convert EML to Other Formats
Convert EML to HTML
To integrate email content into web applications, EML to HTML conversion is the right choice, facilitating visually appealing email presentation.
To convert EML to HTML, you will need the following classes:
- MailMessage class is used to create an object representing an e-mail message. It allows to access message properties, such as subject, body, sender and recipients addresses, etc. With its methods, you can create, load and parse, modify, save emails, or perform other manipulations with them.
- SaveOptions class provides options for saving email messages in various formats. It enables users to customize the way email messages are saved in different formats. With this class, users can specify options for saving attachments, headers, metadata, and properties of email messages, set encoding options or choose whether to save messages with encryption or not.
In the code sample below these classes work together to load an existing EML file and save it as an HTML file using the default HTML saving options:
- Use the MailMessage.load() method to load the existing file into a MailMessage object.
- Save the loaded MailMessage object as an HTML file using the save method. Use SaveOptions.getDefaultHtml() to specify the save options for HTML format.
// Load an existing EML file
MailMessage message = MailMessage.load("myMessage.eml");
message.save("output.html", SaveOptions.getDefaultHtml());
Save EML Resources in a Separate File
Aspose.Email provides the ResourceRenderingMode enumeration allowing developers to manipulate resources in an HTML file. In the code sample below, this enum is used to save resources to a file and insert in HTML the ‘src’ tag with the path to this file:
- Load the email message from the source file using the MapiMessage.load method.
- Create an instance of HtmlSaveOptions with specified rendering and resource options.
- Save the loaded email message as an HTML file to the target location using the save method with the HtmlSaveOptions parameter.
MapiMessage msg = MapiMessage.load("sourceFile.eml");
HtmlSaveOptions htmlSaveOptions = new HtmlSaveOptions();
htmlSaveOptions.setResourceRenderingMode(ResourceRenderingMode.SaveToFile);
htmlSaveOptions.setUseRelativePathToResources(true);
msg.save("target.html", htmlSaveOptions);
Embed Resources to an HTML File
In some cases, it’s required to embed resources, such as images, directly into the HTML file for seamless distribution and presentation. With Aspose.Email for Java, you can easily achieve this using the ResourceRenderingMode enumeration:
- Load the email message from the source file using the MapiMessage.load method.
- Create a new HtmlSaveOptions object and set the resource rendering mode to
EmbedIntoHtml. - Save the loaded email message as an HTML file using the save method, specifying the target file path and passing the HtmlSaveOptions object as a parameter to embed resources into the HTML file.
MapiMessage msg = MapiMessage.load("sourceFile.eml");
HtmlSaveOptions htmlSaveOptions = new HtmlSaveOptions();
htmlSaveOptions.setResourceRenderingMode(ResourceRenderingMode.EmbedIntoHtml);
msg.save("target.html", htmlSaveOptions);
Convert EML to EMLX
EMLX is the message format used by Apple Mail. To convert an EML file to EMLX, load the message and save it with the default EMLX save options:
MailMessage message = MailMessage.load("myMessage.eml");
message.save("output.emlx", SaveOptions.getDefaultEmlx());
Convert EML to ICS
The following code sample demonstrates how to extract calendar event data from an EML file and save it to a separate ICS file for further use or manipulation.
// Load the EML file
MailMessage eml = MailMessage.load("message.eml");
// Find the alternate view with the "text/calendar" media type (ICS)
String icsView = eml.getAlternateViewContent("text/calendar");
// If an ICS view is found, save it to a file
if (icsView != null) {
Files.write(Paths.get("appointment.ics"), icsView.getBytes(StandardCharsets.UTF_8));
}
Customization
Aspose.Email for Java provides tools for customizing ICS (iCalendar) content extracted from EML (Electronic Mail) files.
Customize the event details
The following code sample demonstrates how to set various details of the appointment, such as the summary, location, and description. The code utilizes the Appointment class which represents calendar appointments or events in ICS (iCalendar) format. The class provides properties and methods to create, modify, and manage calendar events programmatically.
// Load the EML file
MailMessage eml = MailMessage.load("message.eml");
// Find the alternate view with the "text/calendar" media type (ICS)
String icsView = eml.getAlternateViewContent("text/calendar");
// If an ICS view is found, load it into an Appointment object
Appointment appointment = Appointment.load(
new ByteArrayInputStream(icsView.getBytes(StandardCharsets.UTF_8)));
// Customize the event details
appointment.setSummary("Customized Event Summary");
appointment.setLocation("Customized Location");
appointment.setDescription("Customized Event Description");
// Add or modify attendees as needed
appointment.getAttendees().clear();
appointment.getAttendees().add("custom@example.com");
// Save the customized ICS content to a file
appointment.save("customized_appointment.ics");
Create a recurrence pattern
The following code sample demonstrates how to create a weekly recurrence pattern for an appointment, where the appointment occurs every 5 weeks on Saturdays. The code utilizes the setRecurrence method of the Appointment class which sets the recurrence pattern.
Appointment appointment = Appointment.load("appointment.ics");
// Repeat every 5 weeks on Saturday
WeeklyRecurrencePattern pattern = new WeeklyRecurrencePattern(5);
pattern.setStartDays(new int[] { DayOfWeek.Saturday });
pattern.setEndDate(new GregorianCalendar(2023, java.util.Calendar.AUGUST, 7).getTime());
appointment.setRecurrence(pattern);
Add EML to MBOX
MBOX is a commonly used format for storing multiple email messages in a single file, making it suitable for email archiving and migration purposes. Use the MboxrdStorageWriter class to write email messages to an MBOX file. The following code sample demonstrates how to perform this task:
try (MailMessage message = MailMessage.load("inputFile.eml");
MboxrdStorageWriter writer = new MboxrdStorageWriter("output.mbox", new MboxSaveOptions())) {
writer.writeMessage(message);
}
Convert EML to MHTML
With Aspose.Email for Java, you can easily convert EML files to MHTML format for various purposes such as archiving, compatibility, offline viewing, etc. Load the email message using MailMessage.load, then use the MhtSaveOptions class as a parameter to the MailMessage.save method to specify the output file format when saving the message as a separate file:
MailMessage message = MailMessage.load("myMessage.eml");
MhtSaveOptions mhtSaveOptions = new MhtSaveOptions();
message.save("output.mhtml", mhtSaveOptions);
The MhtSaveOptions class provides various options for customizing output MHTML files to meet your specific requirements:
setPreserveOriginalDate(boolean)- preserves the original date of the email message during the conversion process.setSaveAttachments(boolean)- preserves the attachments when converting the email to the MHTML format.setSaveAllHeaders(boolean)- writes all the headers of the original message to the output file.
MailMessage message = MailMessage.load("myMessage.eml");
MhtSaveOptions mhtSaveOptions = new MhtSaveOptions();
mhtSaveOptions.setPreserveOriginalDate(true);
mhtSaveOptions.setSaveAttachments(true);
mhtSaveOptions.setSaveAllHeaders(true);
message.save("output.mhtml", mhtSaveOptions);
Convert EML to MSG
Whether you need to migrate email data, archive messages, or integrate with Microsoft Outlook, Aspose.Email provides solutions to achieve your goals. MSG files are widely used by Microsoft Outlook. For EML to MSG conversion, use the MailMessage.load method to load the existing EML file specifying how it will be loaded with EmlLoadOptions.
The following code sample demonstrates how to convert EML files to MSG format:
// Load the mail message
try (MailMessage message = MailMessage.load("sourceFile.eml", new EmlLoadOptions())) {
// Save as MSG
MsgSaveOptions msgSaveOptions = SaveOptions.getDefaultMsgUnicode();
message.save("output.msg", msgSaveOptions);
}
Convert EML to OFT
To convert EML files to Outlook Template (OFT) format, load the existing email message using the MailMessage.load method and save it with MailMessage.save specifying the default options for saving a message to OFT format:
// Load the EML file to be converted
MailMessage message = MailMessage.load("My File.eml");
// Save EML as an OFT
message.save("Saved File.oft", SaveOptions.getDefaultOft());
Add EML to PST
To convert EML files to Personal Storage Table (PST) format, load the message using the MapiMessage.load method, create the output file with PersonalStorage.create and add the email to the created Inbox folder in the storage file using addMessage:
MapiMessage msg = MapiMessage.load("sourceFile.eml", new EmlLoadOptions());
try (PersonalStorage personalStorage = PersonalStorage.create("outputFile.pst", FileFormatVersion.Unicode)) {
FolderInfo inbox = personalStorage.getRootFolder().addSubFolder("Inbox");
inbox.addMessage(msg);
}
Add EML to OST
Developers can easily add EML files to an Outlook Offline Storage Table (OST) file, enabling integration with Microsoft Outlook. The following code sample demonstrates how to add an EML message to an OST file:
try (PersonalStorage ost = PersonalStorage.fromFile("storage.ost", true)) {
// Load the EML file
MapiMessage msg = MapiMessage.load("message.eml", new EmlLoadOptions());
// Add the EML message to the OST file
FolderInfo folderInfo = ost.getRootFolder().getSubFolder("Inbox");
folderInfo.addMessage(msg);
}
The EmlLoadOptions parameter specifies additional options for loading EML files, such as preserving TNEF attachments, setting the preferred text encoding, and more.
PersonalStorage.fromFile(path, true)) whenever you are going to add items to it. A storage opened read-only rejects any write operation.
Convert EML to VCF
Aspose.Email for Java offers functionality to convert EML files to vCard (VCF) format, allowing developers to extract contact information from email messages. For this purpose, the library offers the getAlternateViewContent method of the MailMessage class enabling developers to access alternate views within email messages and extract VCF content embedded within EML files for further use:
// Load the EML file
MailMessage eml = MailMessage.load("message.eml");
// Find the alternate view with the "text/vcard" media type (VCF)
String vcfView = eml.getAlternateViewContent("text/vcard");
// If a VCF view is found, save it to a file
if (vcfView != null) {
Files.write(Paths.get("contact.vcf"), vcfView.getBytes(StandardCharsets.UTF_8));
}