Claudine Maas
Claudine Maas

Reputation: 13

Convert EML to MSG - Java

I want to read the attachments from an EML file. . I prefer converting the EML file to an MSG file so I can re-use the written code, is this possible? If not, is there a way reading attachments from an EML file?

Upvotes: 1

Views: 4769

Answers (2)

Kashif Iqbal
Kashif Iqbal

Reputation: 51

You can use Aspose.Email API to achieve both i.e. EML to MSG conversion and extracting attachments from EML.

Extracting Attachments from 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());
}

Converting EML to 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());

You can further visit Working with MIME Messages for further information in this regard.

I work with Aspose as Developer Evangelist.

Upvotes: 0

haba713
haba713

Reputation: 2677

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.

Then extract attachments like this. See also this explanation.

Upvotes: 1

Related Questions