在本示例中,我们将通过提供配置环境和使用示例代码的详细步骤,重点介绍如何使用 Java **将 Outlook Email 转换为 HTML。您将了解到,为了将 Email 转换为 HTML,可以使用基于 Java 的 API,并且此应用程序可以在任何常用操作系统中使用,例如 Windows、Linux 或 macOS,配置为爪哇。
使用 Java 将 Outlook 电子邮件转换为 HTML 的步骤
- 通过从存储库安装 Aspose.Email JAR 文件来配置您的应用程序
- 实例化 MailMessage 类对象以从磁盘访问源 MSG 或 EML 文件
- 设置输出 HTML 保存和格式选项
- 使用 save 方法将加载的 MSG 或 EML 文件保存为 HTML
在 Java 电子邮件到 HTML 转换应用程序中,可以使用上述步骤轻松开发,其中我们将使用 MailMessage 类对象从磁盘加载源 EML 或 MSG 文件作为第一步。然后,我们将使用 HtmlSaveOptions 对象设置输出 HTML 导出选项,然后将加载的电子邮件文件作为 HTML 保存到磁盘或内存流中。
使用 Java 将 Outlook 电子邮件转换为 HTML 的代码
import com.aspose.email.HtmlFormatOptions; | |
import com.aspose.email.HtmlSaveOptions; | |
import com.aspose.email.MailMessage; | |
import com.aspose.email.ResourceRenderingMode; | |
import com.aspose.email.SaveOptions; | |
public class EmlToHTMLConverter { | |
public static void main2(String[] args) throws Exception {// Exception for MSG to HTML conversion | |
String path= "/Users/KnowledgeBase/TestData/"; | |
// Apply the product license to convert MSG to HTML | |
com.aspose.email.License emaiToHtmllLic = new com.aspose.email.License(); | |
emaiToHtmllLic.setLicense(path+"Conholdate.Total.Product.Family.lic"); | |
// Access the source MSG file from the disk | |
MailMessage emlMessage = MailMessage.load(path+"Message.msg"); | |
// Set the Html rendering options for the output HTML | |
HtmlSaveOptions htmlOptions = SaveOptions.getDefaultHtml(); | |
htmlOptions.setResourceRenderingMode(ResourceRenderingMode.EmbedIntoHtml); | |
// Apply the email message headers in the output HTML using the formatting options | |
htmlOptions.setHtmlFormatOptions(HtmlFormatOptions.WriteHeader | | |
HtmlFormatOptions.WriteCompleteEmailAddress | | |
HtmlFormatOptions.WriteCompleteFromEmailAddress); | |
// Save the loaded MSG/EML as an HTML on the disk | |
emlMessage.save(path+"SaveEmailAsHTML.html", htmlOptions); | |
} | |
} |
为了将 EML 转换为 HTML,Java API 在简单的 API 接口和明确定义的步骤的帮助下使用。 HtmlSaveOptions 可用于通过设置 DefaultHeaderFormat、BeforeHeadersFormat、AfterHeadersFormat、MailMessageSaveType 和 HtmlFormatOptions 等属性来进一步自定义输出 HTML 文件。
在这个简单的示例中,我们学习了使用 Java 将 Outlook 电子邮件转换为 HTML。如果您有兴趣了解如何将电子邮件转换为 Word 文档,请参阅 如何使用 Java 将 Outlook 电子邮件转换为 Word 上的文章。