在这个简单的教程中,我们将通过提供配置环境和使用示例代码的详细步骤,重点介绍如何使用 Java 将 Outlook Email 转换为 Word。您会观察到,为了将 Email 转换为 DOCX,可以使用基于 Java 的 API,并且此应用程序可用于任何常用操作系统,如 Windows、Linux 和 macOS。
使用 Java 将 Outlook 电子邮件转换为 DOCX 的步骤
- 通过从存储库安装 Aspose.Email 和 Aspose.Words JAR 文件来配置应用程序
- 初始化 MailMessage 类对象以从磁盘加载 EML 或 MSG 电子邮件文件
- 将加载的电子邮件 (MSG/EML) 文件作为 ByteArrayOutputStream 保存为中间 MHTML 格式
- 通过将 LoadFormat 设置为 MHTML,初始化 Document 类对象以从流中加载中间 MHTML 文件
- 使用 save 方法将加载的 MHTML 保存到 Docx
按照上述 Java 电子邮件到 Word 转换应用程序中的步骤,我们将使用 MailMessage 类对象作为起点从磁盘加载源 MSG 或 EML 文件,然后将 MSG 文件保存到MHTML 格式的流。最后,来自流的中间 MHTML 将使用 Document 类对象加载并转换为磁盘上的 Docx 文件。
使用 Java 将 Outlook 电子邮件转换为 DOCX 的代码
import com.aspose.email.MailMessage; | |
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
public class EmailToWord { | |
public static void main(String[] args) throws Exception { | |
String filePath = "TestData/"; | |
// Add the Aspose.Email product license to process the Email (MSG/EML) file | |
com.aspose.email.License emailLic = new com.aspose.email.License(); | |
emailLic.setLicense(filePath + "Conholdate.Total.Product.Family.lic"); | |
// Apply the Aspose.Words product license to process the MHTML to DOCX | |
com.aspose.words.License wordsLic = new com.aspose.words.License(); | |
wordsLic.setLicense(filePath + "Conholdate.Total.Product.Family.lic"); | |
// Create an instance of OutputStream object to hold intermediate MHTML | |
ByteArrayOutputStream mhtmlStream = new ByteArrayOutputStream(); | |
// Open the source MSG or EML file from the disk using the MailMessage class | |
MailMessage srcMessage = MailMessage.load(filePath + "Message.msg"); | |
// Export the loaded EML/MSG as MHTML | |
srcMessage.save(mhtmlStream, com.aspose.email.SaveOptions.getDefaultMhtml()); | |
// Instantiate the LoadOptions and set the LoadFormat to Mhtml | |
com.aspose.words.LoadOptions mhtmlLoadOptions = new com.aspose.words.LoadOptions(); | |
mhtmlLoadOptions.setLoadFormat(com.aspose.words.LoadFormat.MHTML); | |
// Create an instance of the Document class to load the MTHML from MemoryStream | |
com.aspose.words.Document document = new com.aspose.words.Document( | |
new ByteArrayInputStream(mhtmlStream.toByteArray()), mhtmlLoadOptions); | |
// Save the MHTML to DOCX using Java | |
document.save(filePath + "SaveEmailAsDocx.docx"); | |
} | |
} |
在上面的示例中,为了将 EML 转换为 Word,Java* API 在简单的 API 接口和清晰定义的步骤的帮助下使用。它对于加载和处理 EML 或 MSG 电子邮件文件格式并将其导出为 MHTML 格式文件非常有用。中间 MHTML 也可以使用相同的 API 保存为图像或 PDF 输出。
在本主题中,我们重点介绍了如何使用 Java 将 Outlook 电子邮件转换为 Docx。如果您希望将 MBOX 文件转换为 PST 存储文件,请参阅 如何在 Java 中将 MBOX 转换为 PST 文件 上的文章。