NegativeArraySizeException when enumerating messages from NSF using Aspose.Email for Java

Hi Aspose Team,

I’m using Aspose.Email for Java 25.5 to extract messages from a Lotus Notes .nsf file.

I’m encountering the following error while calling enumerateMessages():

C:\Users\Admin\Desktop\nsf2eml>javac -cp “.;aspose-email-25.5-jdk16.jar” NsfToEmlAspose.java

C:\Users\Admin\Desktop\nsf2eml>java -cp “.;aspose-email-25.5-jdk16.jar” NsfToEmlAspose “C:\Users\Admin\Desktop\nsf2eml\test.nsf” “C:\Users\Admin\Desktop\nsf2eml\eml”
Aspose license applied.
Unrecoverable error while enumerating messages: java.lang.NegativeArraySizeException
java.lang.NegativeArraySizeException
at com.aspose.email.zass.a(SourceFile:489)
at com.aspose.email.zass.c(SourceFile:481)
at com.aspose.email.zass.a(SourceFile:432)
at com.aspose.email.zass.a(SourceFile:112)
at com.aspose.email.zfv.a(SourceFile:53)
at com.aspose.email.zls.b(SourceFile:323)
at com.aspose.email.zls.a(SourceFile:28)
at com.aspose.email.zls$za.hasNext(SourceFile:411)
at NsfToEmlAspose.main(NsfToEmlAspose.java:48)
Messages loaded: 13, Failed to read during enumeration: 0
Saving message #0: MailVault_alert__Mail_restore__as_zip_file_
Saving message #1: MailVault_alert__Mail_restore__as_zip_file_
Saving message #2: MailVault_alert__File_restore__as_zip_filei
Saving message #3: MailVault_alert__MailVault_startup…
Saving message #4: MailVault_alert__Calendar_restore__as_zipped_.ics_
Saving message #5: MailVault_alert__MailVault_startup…
Saving message #6: MailVault_alert__MailVault_startup…
Saving message #7: MailVault_alert__MailVault_startup…
Saving message #8: MailVault_alert__MailVault_startup…
Saving message #9: MailVault_alert__Calendar_restore__as_zipped_.ics_
Saving message #10: MailVault_alert__Mail_source_problem
Saving message #11: MailVault_alert__MailVault_startup…
Saving message #12: MailVault_alert__MailVault_startup…
Export completed. Total saved: 13, Failed to read during enumeration: 0

I’ve already wrapped the enumerateMessages() call and the iterator in try-catch, but the exception still occurs during hasNext(), causing the program to abort.

My code:

    try {
        NotesStorageFacility nsf = new NotesStorageFacility(nsfPath);
        IGenericEnumerable<MailMessage> messages = nsf.enumerateMessages();

        if (!outputDir.endsWith("\\") && !outputDir.endsWith("/")) {
            outputDir += System.getProperty("file.separator");
        }

        int index = 0;
        for (MailMessage message : messages) {
            if (index >= maxMessages) break;

            try {
                String subject = message.getSubject();
                if (subject == null || subject.trim().isEmpty()) {
                    subject = "No_Subject";
                }

                subject = subject.replaceAll("[^a-zA-Z0-9\\.\\-]", "_");

                String emlPath = outputDir + subject + "_" + index + ".eml";

                System.out.println("Saving message #" + index + ": " + subject);

                message.save(emlPath, SaveOptions.getDefaultEml());
                index++;
            } catch (Exception ex) {
                System.err.println("Skipping message #" + index + " due to error: " + ex.getMessage());
                index++;
            }
        }

        System.out.println("Export completed. Total messages processed: " + index);
    } catch (Exception e) {
        e.printStackTrace();
    }

Aspose crashes with NegativeArraySizeException. Please suggest a workaround or patch?

Regards,
Antony

@antony.aspose

Cause

The NegativeArraySizeException you are encountering typically indicates an issue with the internal handling of message enumeration in the .nsf file. This can occur due to various reasons, such as corrupted data or unexpected structures within the file.

Solution

To troubleshoot and resolve this issue, follow these steps:

  1. Update Aspose.Email: Ensure you are using the latest version of Aspose.Email for Java. The version you are using (25.5) is outdated, and many bugs may have been fixed in later releases.

  2. Check for Corruption: Verify the integrity of the .nsf file. If possible, open it in Lotus Notes to see if it functions correctly there.

  3. Limit Message Enumeration: If the file is large, limit the number of messages you are enumerating. Modify your loop to skip certain messages or use a filter.

  4. Error Handling: Ensure that your try-catch block logs the specific messages that cause the exception. This will help identify problematic messages.

  5. Contact Support: If the issue persists, reach out to Aspose support with the specific .nsf file and the error details for further assistance.

Code

Here’s a modified version of your code that includes logging for skipped messages:

try {
    NotesStorageFacility nsf = new NotesStorageFacility(nsfPath);
    IGenericEnumerable<MailMessage> messages = nsf.enumerateMessages();

    if (!outputDir.endsWith("\\") && !outputDir.endsWith("/")) {
        outputDir += System.getProperty("file.separator");
    }

    int index = 0;
    for (MailMessage message : messages) {
        if (index >= maxMessages) break;

        try {
            String subject = message.getSubject();
            if (subject == null || subject.trim().isEmpty()) {
                subject = "No_Subject";
            }

            subject = subject.replaceAll("[^a-zA-Z0-9\\.\\-]", "_");
            String emlPath = outputDir + subject + "_" + index + ".eml";

            System.out.println("Saving message #" + index + ": " + subject);
            message.save(emlPath, SaveOptions.getDefaultEml());
            index++;
        } catch (Exception ex) {
            System.err.println("Skipping message #" + index + " due to error: " + ex.getMessage());
            index++;
        }
    }

    System.out.println("Export completed. Total messages processed: " + index);
} catch (Exception e) {
    e.printStackTrace();
}

Next Steps

  1. Update to the latest version of Aspose.Email.
  2. Check the .nsf file for corruption.
  3. Implement logging for skipped messages to identify any specific issues.
  4. If the problem continues, contact Aspose support for more detailed assistance.

Please proceed with these steps and let me know if you need further help!

Hi Team,

Thanks for your recent update and the fixes applied to Aspose.Email for Java.

Unfortunately, the issue still persists:

My .nsf file contains ~9,000 messages

Enumeration using enumerateMessages() fails at around message #13 with the same NegativeArraySizeException

Once the error occurs, the iterator cannot recover — and the entire message loop breaks

Competing tools like SysTools NSF Converter (Demo) are able to extract all 25+ messages from the same file successfully

Attached source code also.

Regards,
Antony
NsFToEmlAspose.zip (1.4 KB)

Hello @antony.aspose,

Thank you for your message and detailed report.

Unfortunately, due to the lack of a publicly available NSF file format specification, support for reading Lotus Notes .nsf files in Aspose.Email is inherently limited.
Our support remains limited and may fail at certain points. The errors you’re encountering stem from these constraints, as noted in our documentation (Working with IBM Notes):

  • NSF files created by Notes 7 and above may not work reliably
  • Files using LZ1 compression are problematic. That compression algorithm can’t be parsed.
  • Only message reading is implemented. Folder structures within the NSF file are not supported, due to a lack of format specification.

Regarding the third-party tool you mentioned (e.g., SysTools NSF Converter), it’s worth noting that “Lotus Notes Installation is Required to Perform Conversion”, according to their product page. This likely means the tool relies on IBM’s proprietary Lotus Notes APIs, which provide full access to the NSF file internals when the Notes client is present.
This approach is different from Aspose.Email, which operates independently without requiring Lotus Notes or its APIs.

We are sorry for the inconvenience this has caused.