将 EML 转换为 MSG - Java

Convert EML to MSG - Java

我想从 EML 文件中读取附件。 .我更喜欢将 EML 文件转换为 MSG 文件,这样我就可以重新使用编写的代码,这可能吗?如果没有,有没有办法从 EML 文件中读取附件?

If not, is there a way reading attachments from an EML file?

JavaMail supports reading EML files (MIME-type message/rfc822). See the example here

然后提取附件like this. See also this explanation

您可以使用 Aspose.Email API 来实现两者,即 EML 到 MSG 的转换和从 EML 中提取附件。

正在从 EML 中提取附件

//Initialize and Load an existing EML file
MailMessage msg = MailMessage.load(dataDir + "EmailWithAttachment.eml", new EmlLoadOptions());

//Initialize AttachmentCollection object with MailMessage Attachments
AttachmentCollection attachments = msg.getAttachments();

//Iterate over the AttachmentCollection
for (int index = 0; index < attachments.size(); index++) {
    //Initialize Attachment object and Get the indexed Attachment reference
    Attachment attachment = (Attachment) attachments.get_Item(index);
    //Display Attachment Name
    System.out.println(attachment.getName());
    //Save Attachment to disk
    attachment.save(dataDir + "attachment_" + attachment.getName());
}

将 EML 转换为 MSG

// Initialize and Load an existing EML file by specifying the MessageFormat
MailMessage eml = MailMessage.load(dataDir + "test.eml");
//Save the Email message to disk in Unicode format
eml.save(dataDir + "LoadingEMLSavingToMSG_out.msg", SaveOptions.getDefaultMsgUnicode());

您可以进一步访问Working with MIME Messages了解这方面的更多信息。

我在 Aspose 工作,担任开发人员布道师。