Hi,
Hi Satish,
Thank you for contacting support team.
I have tested the sample PST file and tried to open it in Outlook but could not succeed as Outlook gives error “Errors have been detected in the file” and does not open it. Similarly Aspose.Email throws exception “Header CRC is not valid” due to errors in PST files.
I tried to re-produce the issue by following the steps as given below:
- Create PST
- Create custom folder
- Load EML
- Convert to MapiMessage
- Add MSG to customer folder
- Close the PST
- Open the same PST again
- Access the custom folder
- Add some MSG again
- Close the PST
It is observed that no exception is raised by Outlook or Aspose library, while processing same PST multiple times in the following sample code.
In your scenario, as you have mentioned that your process terminates abruptly, so the PST file format may not remain intact which causes Outlook and Aspose library to throw exception. Therefore, it is suggested that you may please try to terminate the thread until and unless the last activity, say adding a message, is completed and PST file is properly disposed off. It will cause the PST file format to remain proper, and hence it will not raise exception while opening the same PST next time.
Following is the sample code which is used to create and reuse the PST file without any exception.
PersonalStorage pst = PersonalStorage.create("D:\\Aspose\\newpst3\\Sample.pst", FileFormatVersion.Unicode);
FolderInfo testFolder = pst.getRootFolder().addSubFolder("Test");
MailMessage mail = MailMessage.load("D:\\Aspose\\newpst3\\Test.eml");
MapiMessage mapi = MapiMessage.fromMailMessage(mail);
testFolder.addMessage(mapi);
pst.dispose();
PersonalStorage pst2 = PersonalStorage.fromFile("D:\\Aspose\\newpst3\\Sample.pst");
testFolder = pst2.getRootFolder().getSubFolder("Test");
testFolder.addMessage(mapi);
pst2.dispose();
Please feel free to write us back if you have any other query related to Aspose.Email. We will be glad to provide you assistance as soon as possible.
Thanks Kashif Iqbal!
Hi Satish,
Hi Kashif,
Hi Satish,
static void CreatePSTAndAddMessageToPST()
{
ByteArrayOutputStream BAoutput = new ByteArrayOutputStream();
PersonalStorage pst = PersonalStorage.create(BAoutput, com.aspose.email.FileFormatVersion.Unicode);
try
{
pst.getRootFolder().addSubFolder(“TestItems”);
FolderInfo folder = pst.getRootFolder().getSubFolder(“TestItems”);
MapiMessage msg = new MapiMessage("from@domain.com","to@domain.com", “Subject”, “Body”);
folder.addMessage(msg);
}
finally
{
pst.dispose();
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(“Output.pst”);
fos.write(BAoutput.toByteArray());
fos.flush();
fos.close();
}
catch (java.io.IOException e)
{
e.printStackTrace();
}
}
}
Hi Satish,