Hi there,
I am getting Out of Memory error every time when I ingest some large set of messages (lets say 1000 messages) to create a pst file. I am passing OutputStream instance, instead of direct file location, to create a PersonalStorage object. i.e.
OutputStream outputStream = new FileOutputStream("file_name.pst");
PersonalStorage personalStorage = PersonalStorage.create(outputStream, FileFormatVersion.Unicode);
After processing some of the messages, at some point, the program fails with OoM error near FolderInfo.addMessage()
function. Looks like newly generated pst file is constantly increasing the heap space and at some point it reaches the thrashhold and fails with Out of Memory error.
Please suggest how I can avoid such error. Here is my full program -
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.aspose.email.EmlLoadOptions;
import com.aspose.email.FileFormatVersion;
import com.aspose.email.FolderInfo;
import com.aspose.email.MapiMessage;
import com.aspose.email.PersonalStorage;
public class TestPstExport {
public static void main(String []args) throws IOException {
createPSTFromOutstream();
System.exit(0);
}
public static void createPSTFromOutstream() {
InputStream inputStream = null;
OutputStream outputStream = null;
PersonalStorage personalStorage = null;
MapiMessage mapiMessage = null;
String inputFileLocation = "D:/emailSamples/testmail.eml";
try {
outputStream = new FileOutputStream("D:/exports/pst_export_file.pst");
checkAsposeLicense();
personalStorage = PersonalStorage.create(outputStream, FileFormatVersion.Unicode);
FolderInfo exportFolder = personalStorage.getRootFolder().addSubFolder("export");
for (int i= 1; i <= 1000; i++) {
System.out.println("Please wait while loading eml file " + i + "...");
// taking same file again
inputStream = new FileInputStream(inputFileLocation);
mapiMessage = MapiMessage.load(inputStream, new EmlLoadOptions());
// updating subject because injecting same file again and again
mapiMessage.setSubject(mapiMessage.getSubject() + "_00" + i);
System.out.println("Please wait while writing messages to the PST " + i + "...");
exportFolder.addMessage(mapiMessage);
System.out.println("Message " + i + " is added to PST file!");
outputStream.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
personalStorage.dispose();
personalStorage.close();
}
}
public static void checkAsposeLicense() throws RuntimeException {
System.out.println("Checking for aspose licence...");
File licenseFile = new File("D:/license/Aspose.Email.Java.lic");
if(!licenseFile.exists()) {
throw new RuntimeException("PST license file does not exists.");
}
new com.aspose.email.License().setLicense(licenseFile);
System.out.println("License applied successfully!");
}
}