将 HTML 转换为其他格式
将 HTML 转换为 EML
Aspose.Email for Java 提供了一种使用 MailMessage.load 和 MailMessage.save 方法分别用于加载现有 HTML 文件并以 EML 格式保存:
MailMessage eml = MailMessage.load("myContent.html", new HtmlLoadOptions());
eml.save("output.eml", SaveOptions.getDefaultEml());
在代码示例中, HtmlLoadOptions 类允许在从 HTML 格式加载 MailMessage 时指定附加选项。以下代码示例演示了此类的使用。在示例中,它请求消息正文的纯文本表示形式:
// 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);
shouldAddPlainTextView(boolean),而不是 setShouldAddPlainTextView.
将 HTML 转换为 EMLX
您可以轻松将 HTML 文件转换为 EMLX。此类转换的所有选项和类同样适用于此类型的转换:
MailMessage eml = MailMessage.load("myContent.html", new HtmlLoadOptions());
eml.save("output.emlx", SaveOptions.getDefaultEmlx());
有关其他设置,请参见 将 HTML 转换为 EML 段落。
将 HTML 转换为 ICS
要执行此任务,库提供了 Appointment 类来表示和管理日历事件。以下代码示例演示如何从 HTML 内容创建约会并保存为 ICS(iCalendar)文件格式:
// 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);
从 HTML 内容生成 MBOX
要执行 HTML 到 MBOX 的转换,请使用 load 方法的 MailMessage 类,指定 HTML 内容文件路径以及一个 HtmlLoadOptions。此方法解析 HTML 内容并生成相应的 MailMessage 对象,保留原始 HTML 的结构和格式。将 HTML 内容加载到 MailMessage 对象后,使用 MboxrdStorageWriter 类:
try (MailMessage eml = MailMessage.load("content.html", new HtmlLoadOptions());
MboxrdStorageWriter writer = new MboxrdStorageWriter("output.mbox", new MboxSaveOptions())) {
writer.writeMessage(eml);
}
将 HTML 转换为 MHTML
使用 load 方法的 MailMessage 类加载现有文件,指定其路径以及一个 HtmlLoadOptions。在将 HTML 内容加载到 MailMessage 对象后,开发者可以使用 保存 方法,指定所需的输出文件路径并使用 SaveOptions.getDefaultMhtml() 选项:
MailMessage eml = MailMessage.load("content.html", new HtmlLoadOptions());
eml.save("output.mhtml", SaveOptions.getDefaultMhtml());
该 MhtSaveOptions 类提供了多种选项,用于配置输出 MHTML 文件的行为和设置,而不是 SaveOptions.getDefaultMhtml()。其中最常用的是:
MhtFormatOptions- 允许您自定义邮件在 MHT 格式下的保存方式,以最符合您的需求。SaveAttachments- 获取或设置一个值,指示是否保存附件。SaveAllHeaders- 定义是否在输出 MHTML 中保存所有头信息。默认值为 false。PreserveOriginalDate- 定义在保存邮件时是否保留原始日期。默认值为 true。
以下代码示例演示了如何使用这些属性:
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);
将HTML转换为MSG
在将 HTML 内容加载到 MailMessage 对象后,使用以下方式将其保存为 MSG 文件: 保存 方法,指定所需的输出文件路径并使用 SaveOptions.getDefaultMsgUnicode() 选项。此选项确保输出文件以 Unicode 编码的 MSG 格式保存。
MailMessage eml = MailMessage.load("content.html", new HtmlLoadOptions());
eml.save("output.msg", SaveOptions.getDefaultMsgUnicode());
将HTML转换为OFT
在将 HTML 内容加载到 MailMessage 对象后,使用以下方式将其保存为 OFT 文件: 保存 方法,指定所需的输出文件路径并使用 SaveOptions.getDefaultOft() 选项:
MailMessage eml = MailMessage.load("content.html", new HtmlLoadOptions());
eml.save("template.oft", SaveOptions.getDefaultOft());
将源 HTML 内容添加为消息到 PST
HTML 到 PST 的转换涉及创建一个带有新文件夹的 PST(个人存储表)文件,加载 HTML 文件并将其添加到新文件夹中:
- 使用以下方式创建表示新 PST 文件的 PersonalStorage 对象实例: create 方法的 PersonalStorage 类。
- 使用以下方式访问 PST 文件的根文件夹并向其添加子文件夹: addSubFolder 方法的 FolderInfo 类。
- 将 HTML 文件的内容加载到一个 MapiMessage 使用… 对象 load 使用… 实例的方法 HtmlLoadOptions 以指定内容为 HTML 格式。
- 使用以下方式将已加载的 MapiMessage 对象(表示 HTML 内容)添加到 PST 文件中的文件夹: addMessage 方法。
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);
}
将源 HTML 内容添加为消息到 OST
以下代码示例及步骤将展示这些组件如何协同工作,将 HTML 内容添加到 OST 文件中:
- 使用以下方式加载现有的 OST 文件: fromFile 方法的 PersonalStorage 类。传入
true作为第二个参数打开存储进行写入。 - 使用以下方式加载 HTML 文件: load 方法的 MapiMessage 表示 Microsoft Outlook 格式电子邮件消息的类。
- 指定 HtmlLoadOptions 以在从 HTML 格式加载消息时启用附加选项。
- 使用以下方式检索 OST 文件的根文件夹:
getRootFolder()PersonalStorage 对象的方法。 - 使用以下方式获取 OST 文件中的收件箱文件夹:
getSubFolder根文件夹上的方法。 - 使用以下方式将已加载的 MapiMessage 对象(表示 HTML 内容)添加到收件箱文件夹:
addMessage文件夹上的方法。
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);
}
将HTML转换为VCF
以下代码示例演示如何创建联系人项、用 HTML 内容填充并将其保存为 VCF 文件:
- 将 HTML 文件的内容读取到字符串变量中。
- 通过实例化以下内容创建新的 MapiContact 对象: MapiContact 类。
- 设置联系人的属性:显示名称、电子邮件地址。
- 使用以下方式将联系人的正文内容设置为 HTML:
setBodyContent. - 使用以下方式将联系人保存为 VCF 文件: 保存 方法。
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);