Java code to read outlook PST files to get mail address in TO, CC

127 views Asked by At

I want to get the list of all email as unique addresses by using a library named Java libpst. The problem is that the library suggest to read all the message from the root folder but what about the scenarios when the user creates their own folders to group the emails.

I am using the Java libpst to read the mail addresses from PST files, which suggest to apply the following code:

PSTMessage email = (PSTMessage)pstFile.getRootFolder().getNextChild();

I understand the root folder is the default folder like the inbox, but what about the sent, or other folders created by the owner. Should it be better to iterate all the message in root folder and then goes to all the message for each new subfolder?

Vector<PSTFolder> folderList = pstFile.getRootFolder().getSubFolders();
1

There are 1 answers

0
James McLeod On

The function pstFile.getRootFolder().getNextChild(); returns an object of type PSTObject, which might be a PSTMessage (which includes appointments and contacts as well as actual messages), but could also be a PSTFolder.

One way to do this is with a loop:

// ...
processFolder(pstFile.getRootFolder());
// ...

with processFolder looking something like this (warning untested code!):

function void processFolder(PSTFolder folder)
{
    do {
        PSTObject pstObject = folder.getNextChild();
        if (pstObject == null) {
            break;
        }
        if (pstObject instanceof PSTMessage) {
            processMessage((PSTMessage)object);
        } else if (pstObject instanceof PSTFolder) {
            processFolder((PSTFolder)object)
        }
    }
}