We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Extract messages form pst file in Java - NullPointerException error

Aspose Team,

We’re trying to extract messages from PST file with the Aspose.Email Java library (version 20.5). We got the NullPointerException error when extracting some of the messages form the sample pst file. Following is the stack trace.

java.lang.NullPointerException
at com.aspose.email.zapz.d(SourceFile:2536)
at com.aspose.email.zapz.b(SourceFile:307)
at com.aspose.email.zapz.d(SourceFile:1350)
at com.aspose.email.zzc.h(SourceFile:526)
at com.aspose.email.zzc.(SourceFile:98)
at com.aspose.email.zavb.(SourceFile:66)
at com.aspose.email.zahh.a(SourceFile:321)
at com.aspose.email.zavk.b(SourceFile:346)
at com.aspose.email.zavk.a(SourceFile:305)
at com.aspose.email.PersonalStorage.extractMessage(SourceFile:627)
at com.aspose.email.PersonalStorage.extractMessage(SourceFile:643)
at ExtractPst.extractFolder(ExtractPst.java:32)
at ExtractPst.extractFolder(ExtractPst.java:43)
at ExtractPst.extractFolder(ExtractPst.java:43)
at ExtractPst.extractFolder(ExtractPst.java:43)
at ExtractPst.extractFolder(ExtractPst.java:43)
at ExtractPst.main(ExtractPst.java:19)

Following is the sample code for reproducing the problem. Please advise if we miss something or there is a bug in the library.

import com.aspose.email.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;

public class ExtractPst {

private static int messageNumber = 0;

public static void main(String[] args) {
    try {
        String filepath = "/home/ubuntu/winshare/Sample_Files/Archives/Pst/NullPointerException.pst";
        String outpath = "/home/ubuntu/winshare/temp/pstout4";

        PersonalStorage pst = PersonalStorage.fromFile(filepath, false);
        FolderInfo rootFolder = pst.getRootFolder();
        extractFolder(pst, rootFolder, Paths.get(outpath));
    }
    catch(Throwable ex) {
        ex.printStackTrace();
    }
}

private static void extractFolder(PersonalStorage pst, FolderInfo folder, Path outDirectory) throws Exception {
    Iterator<String> iterable = folder.enumerateMessagesEntryId().iterator();
    while(iterable.hasNext()){
        String messageEntryId = iterable.next();
        String fileName = Paths.get(outDirectory.toString(), String.valueOf(messageNumber)).toString();
        System.out.println(String.format("Processing messageEntryId %s", messageEntryId));
        MapiMessage message = pst.extractMessage(messageEntryId);
        IMapiMessageItem item = message.toMapiMessageItem();
        if (item instanceof MapiContact) {
            ((MapiContact) item).save(fileName + ".vcf", 0);
        } else {
            message.save(fileName + ".msg", new MsgSaveOptions(MailMessageSaveType.getOutlookMessageFormat()));
        }
        messageNumber++;
    }

    for (FolderInfo subFolder : folder.getSubFolders()) {
        extractFolder(pst, subFolder, outDirectory);
    }
}

}

Thank you for your help.
Xiaohong Yang

@xyang,

I have tested the PST file on my end and have observed the issue specified. A ticket with ID EMAILJAVA-34709 has been created in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so we may share notification with you once issue will be fixed.

@xyang,

We have worked with PST file on our end. Actually, the issue is in source PST file which is corrupted as some message data blocks are not found. Even MS Outlook also shows an error when trying to display some messages.

Therefore, we have modified the thrown exception message to a more user-friendly manner. The following code sample shows how to handle the exceptions:

for (String messageEntryId : folder.enumerateMessagesEntryId()) {
    try {
        MapiMessage message = pst.extractMessage(messageEntryId);

        IMapiMessageItem item = message.toMapiMessageItem();

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            if (item instanceof MapiContact) {
                ((MapiContact)item).save(os, ContactSaveFormat.VCard);
            } else {
                message.save(os, new MsgSaveOptions(MailMessageSaveType.getOutlookMessageFormat()));
            }
        } finally {
            os.close();
        }

        totalMessageCount++;
    } catch (Exception e) {
        System.err.println(e);
        corruptedMessageCount++;
    }
} 

Outlook.jpg (117.3 KB)