How to Add EML to PST directly?

I am writing some code that generates a PST from EML files. Using Aspose.Email, as far as I can tell, the only way for me to get the EML into the PST is to

1) Read the EML from disk into MailMessage
2) Save the EML as a MSG using MailMessage.Save
3) Read the MSG from disk
4) Add the MSG to a PST

It would be nice if I could just

1) Read the EML from disk
2) Add the EML to a PST

so that I can skip the step of having to write out the temporary MSG file.

Hi,


Thank you for contacting support.

I am afraid the current functionality of Aspose.Email for Java only allows to add Outlook messages to existing or new PersonalStorage files. If you require to add EML files to PersonalStorage then you have to convert such messages to Outlook format first. Although there is no need to save the converted messages to disk before adding them to PersonalStorage. You can load the EML file in MailMessage instance and then load the same message to MapiMessage using the MapiMessage.FromMailMessage method.

Please check the below source code for your kind reference

C#

PersonalStorage pst = PersonalStorage.create(“sample.pst”, FileFormatVersion.Unicode);

pst.getRootFolder().addSubFolder(“EmailTestFolder”).addMessage(MapiMessage.fromMailMessage(MailMessage.load(“test.eml”)));


Please write back in case of any ambiguities or comments.
Regards,

So when I try to add it directly, using similar code to what you provided, the resulting emails within the newly created PST are “corrupted”. When looking at them in summary view, you can’t see some of the information (sender, date, etc.). These are EMLs that I have validated using MailMessage.validateMessage().

However, when I first write them to disk as MSG and then reload the MSG and add that to the PST, everything looks great.

This DOES work:

pst = PersonalStorage.create("/data/sample.PST", FileFormatVersion.Unicode);
folder = pst.getRootFolder().addSubFolder(FOLDER_NAME);
sourceEmlPath = f.getAbsolutePath();
EmlValidationErrorCollection emlValidationErrorCollection = MailMessage.validateMessage(sourceEmlPath);

if (emlValidationErrorCollection.size() > 0) {
validationErrors++;
}

MailMessage mailMessage = MailMessage.load(sourceEmlPath, MessageFormat.getEml());
File tmpMsgFile = TempFileManager.createTempFile("pst-exporter-" + UUID.randomUUID().toString(), ".msg");
mailMessage.save(tmpMsgFile.getAbsolutePath(), MailMessageSaveType.getOutlookMessageFormatUnicode());
MapiMessage mapiMessage = MapiMessage.fromFile(tmpMsgFile.getAbsolutePath());


try {
folder.addMessage(mapiMessage);
} catch (Exception e) {
e.printStackTrace();
}

tmpMsgFile.delete();


This DOES NOT work:

pst = PersonalStorage.create("/data/sample.PST", FileFormatVersion.Unicode);
folder = pst.getRootFolder().addSubFolder(FOLDER_NAME);
sourceEmlPath = f.getAbsolutePath();
EmlValidationErrorCollection emlValidationErrorCollection = MailMessage.validateMessage(sourceEmlPath);

if (emlValidationErrorCollection.size() > 0) {
validationErrors++;
}

try {
folder.addMessage(MapiMessage.fromMailMessage(MailMessage.load(sourceEmlPath, MessageFormat.getEml())));
} catch (Exception e) {
e.printStackTrace();
}

Hi,


Thank you for writing back.

I am afraid, I am unable to replicate the said issue. Moreover, I have provided you the sample code after testing it on my end. Attached is a sample EML file along with the resultant PST that I have created using the sample code that you have mentioned as “Does Not Work”. Please note that I have used Aspose.Email for Java v1.9.0 to create the PST and Outlook 2010 32 bit to open the PST.

If you are using some older version of Aspose.Email for Java component, I would request you to give the latest Jars a try and feed us back with your results. In case your problem persists then please share the version of Outlook application that you are using to open the PST file, along with some of your EML files for our review.

Regards,

I was using Aspose.Email 1.8.0 for Java, but this morning I have downloaded 1.9.0 and am using it.

Unfortunately, I still get the same behavior. I copy/pasted your code and used the attached EML. When attempting to open the new PST in Microsoft Office I see the message but it is missing some metadata, and then immediately Outlook crashes.

You will find attached…
Screenshot of Outlook Version
Screenshot of Outlook opened to the sample PST
The sample source EML

Hi,


Thank you for sharing the details with us.

The problem is strange to me cause I am using the same version of Outlook application with exception that my product is licensed whereas your Outlook version seems to be running in Trial mode from the screenshot shared by you. I am further investigating the fact if trial version of Outlook 2010 behaves differently in your given scenario. I will keep you posted with updates on this.

Moreover, we would like to have more details regarding your development environment such as JRE version, IDE, OS version/service packs etc. This information will help us to simulate your environment to reproduce the problem on our end and raise a ticket for it.

Thank you for your understanding and cooperation.

PS: Please check the attached snapshots for your reference.