How to parse the below values from the Java Code for Aspose.Email

Hello Team,
Can you help me to know how to fetch the below properties from the .PST file from the code given below.

  1. Subfolders Data - How to parse getSubFolders()
  2. To, Cc, Bcc email addresses
  3. HTML Body Content
  4. Attachment & Inline Images Info
  5. Conversation Data
public static void main(String[] args) {
        String pstFileName = dataDir + "Sample1.pst";
        PersonalStorage pst = PersonalStorage.fromFile(pstFileName);
        FolderInfo folderInfo = pst.getRootFolder();
        displayFolderContents(folderInfo, pst);
    }

    private static void displayFolderContents(FolderInfo folderInfo, PersonalStorage pst) {
        // display the folder name
        System.out.println("<====================================================>");
        System.out.println("Folder: " + folderInfo.getDisplayName());
        System.out.println("Sub Folders List: " + folderInfo.getSubFolders());
        System.out.println("<====================================================>");
        // display information about messages inside this folder
        MessageInfoCollection messageInfoCollection = folderInfo.getContents();
        for (int i = 0; i < messageInfoCollection.size(); i++) {
            MessageInfo messageInfo = (MessageInfo) messageInfoCollection.get_Item(i);
            System.out.println("--------------------------------------------");
            System.out.println("Subject: " + messageInfo.getSubject());
            System.out.println("Sender: " + messageInfo.getSenderRepresentativeName());
            System.out.println("To: " + messageInfo.getDisplayTo());
            System.out.println("CC: " + messageInfo.getDisplayCC());
            System.out.println("EntryID: " + messageInfo.getEntryIdString());
            System.out.println("--------------------------------------------");
        }
    }

@MaazHussain

To fetch the specified properties from a PST file using Aspose.Email in Java, you can utilize the following methods based on the provided code snippet. Here’s how you can retrieve each of the requested properties:

  1. Subfolders Data: You can retrieve subfolders using the getSubFolders() method of the FolderInfo class. In the provided code, this is already demonstrated. You can iterate through the subfolders as follows:

    if (folderInfo.hasSubFolders()) {
        for (int i = 0; i < folderInfo.getSubFolders().size(); i++) {
            FolderInfo subfolderInfo = (FolderInfo) folderInfo.getSubFolders().get_Item(i);
            System.out.println("Subfolder: " + subfolderInfo.getDisplayName());
            // You can call displayFolderContents(subfolderInfo, pst) to display its contents recursively
        }
    }
    
  2. To, Cc, Bcc Email Addresses: The MessageInfo class provides methods to get the “To”, “Cc”, and “Bcc” email addresses. You can use getDisplayTo(), getDisplayCC(), and getDisplayBCC() methods respectively. Here’s how you can print these addresses:

    System.out.println("To: " + messageInfo.getDisplayTo());
    System.out.println("CC: " + messageInfo.getDisplayCC());
    System.out.println("BCC: " + messageInfo.getDisplayBCC());
    
  3. HTML Body Content: To retrieve the HTML body content of an email, you can use the getBodyHtml() method of the MessageInfo class. Here’s an example:

    System.out.println("HTML Body: " + messageInfo.getBodyHtml());
    
  4. Attachment & Inline Images Info: You can access attachments using the getAttachments() method of the MessageInfo class. To get information about attachments and inline images, you can iterate through the attachments collection:

    AttachmentInfoCollection attachments = messageInfo.getAttachments();
    for (int j = 0; j < attachments.size(); j++) {
        AttachmentInfo attachment = attachments.get_Item(j);
        System.out.println("Attachment Name: " + attachment.getName());
        System.out.println("Is Inline: " + attachment.isInline());
    }
    
  5. Conversation Data: To retrieve conversation data, you can use the getConversationIndex() method of the MessageInfo class. This will give you the conversation index for the message:

    System.out.println("Conversation Index: " + messageInfo.getConversationIndex());
    

By integrating these snippets into your existing code, you can effectively parse the required properties from the PST file.

Example Code Integration

Here’s how you might integrate all of the above into your displayFolderContents method:

private static void displayFolderContents(FolderInfo folderInfo, PersonalStorage pst) {
    System.out.println("Folder: " + folderInfo.getDisplayName());
    MessageInfoCollection messageInfoCollection = folderInfo.getContents();
    for (int i = 0; i < messageInfoCollection.size(); i++) {
        MessageInfo messageInfo = (MessageInfo) messageInfoCollection.get_Item(i);
        System.out.println("Subject: " + messageInfo.getSubject());
        System.out.println("Sender: " + messageInfo.getSenderRepresentativeName());
        System.out.println("To: " + messageInfo.getDisplayTo());
        System.out.println("CC: " + messageInfo.getDisplayCC());
        System.out.println("BCC: " + messageInfo.getDisplayBCC());
        System.out.println("HTML Body: " + messageInfo.getBodyHtml());
        
        AttachmentInfoCollection attachments = messageInfo.getAttachments();
        for (int j = 0; j < attachments.size(); j++) {
            AttachmentInfo attachment = attachments.get_Item(j);
            System.out.println("Attachment Name: " + attachment.getName());
            System.out.println("Is Inline: " + attachment.isInline());
        }
        
        System.out.println("Conversation Index: " + messageInfo.getConversationIndex());
    }

    if (folderInfo.hasSubFolders()) {
        for (int i = 0; i < folderInfo.getSubFolders().size(); i++) {
            FolderInfo subfolderInfo = (FolderInfo) folderInfo.getSubFolders().get_Item(i);
            displayFolderContents(subfolderInfo, pst);
        }
    }
}

This code will help you extract all the required information from the PST file effectively.

Sources

Getting an error called

Cannot resolve method

For all these

getDisplayBCC()
getBodyHtml()
getAttachments()
getConversationIndex()
attachment.isInline()

Hello @MaazHussain ,

Could you check code sample below:

    public static void main(String[] args) {
        String pstFileName = dataDir + "Sample1.pst";
        PersonalStorage pst = PersonalStorage.fromFile(pstFileName);
        FolderInfo folderInfo = pst.getRootFolder();
        displayFolderContents(folderInfo, pst);
    }

    private static void displayFolderContents(FolderInfo folderInfo, PersonalStorage pst) {
        // display the folder name
        System.out.println("<====================================================>");
        System.out.println("Folder: " + folderInfo.getDisplayName());
        System.out.println("<====================================================>");
        // display information about messages inside this folder
        MessageInfoCollection messageInfoCollection = folderInfo.getContents();
        for (int i = 0; i < messageInfoCollection.size(); i++) {
            MessageInfo messageInfo = messageInfoCollection.get_Item(i);
            System.out.println("--------------------------------------------");
            System.out.println("Subject: " + messageInfo.getSubject());
            System.out.println("Sender: " + messageInfo.getSenderRepresentativeName());
            System.out.println("To: " + messageInfo.getDisplayTo());
            System.out.println("CC: " + messageInfo.getDisplayCC());
            System.out.println("EntryID: " + messageInfo.getEntryIdString());

            // extract message from PST
            MapiMessage msg = pst.extractMessage(messageInfo.getEntryIdString());
            System.out.println("Text body: " + msg.getBody());
            System.out.println("Html body: " + msg.getBodyHtml());
            for (MapiRecipient recipient : msg.getRecipients()) {
                System.out.println("Recipient: ");
                System.out.println(recipient.getDisplayName());
                System.out.println(recipient.getEmailAddress());
                // MapiRecipientType.MAPI_TO, MapiRecipientType.MAPI_CC, MapiRecipientType.MAPI_BCC
                if (recipient.getRecipientType() == MapiRecipientType.MAPI_BCC) {
                    System.out.println("BCC recipient");
                }
            }
            System.out.println("Conversation Topic: " + msg.getConversationTopic());
            for (MapiAttachment attachment : msg.getAttachments()) {
                System.out.println("Attachment: " + attachment.getDisplayName());
                if (attachment.isInline()) {
                    System.out.println("Inline attachment");
                }
            }
            System.out.println("--------------------------------------------");
        }
        //display information about sub-folders
        // !!! recursive call !!!
        for (FolderInfo subfolder : folderInfo.getSubFolders()) {
            displayFolderContents(subfolder, pst);
        }
    }

Thanks @sergey.vivsiuk

Can you let me know how i can connect this Java code to an XCode project?
Please share a working example of the same

Hello @MaazHussain ,

You can run your Java application as a backend service and communicate with it from the Xcode project via HTTP or WebSockets.
You could also consider using Aspose.Email for C++ for integration with the Xcode project.

(We currently release Aspose.Email for C++ for macOS, but only for x64 platforms. We do not provide a version for ARM architectures.)

Okay @sergey.vivsiuk will run Java application itself as a backend service and we will communicate, since we are looking for both architectures.

Also we are looking to purchase Aspose.Email package for Java in our organisation, but with the code you provided above I am able to parse everything, can you let me know what I am missing here in the paid version? My main motive is to parse the PST/OST files.

Hello @MaazHussain,

The fully licensed version offers benefits that go beyond the functionality of trial or limited versions.

  • No restrictions on the number of items processed in PST/OST files. (Only 50 emails can be extracted from a folder in PST file in the evaluation version)
  • No restrictions on the number of attachments in messages. (Only 3 attachments as well as inline images can be extracted from a message in the evaluation version)
  • Access to regular updates and bug fixes.

Thanks for the clarification @ margarita.samodurova

@MaazHussain ,

You’re welcome!