jumpa
jumpa

Reputation: 678

javamail and gmail

  1. The following code throws IndexOutOfBoundsException. Any idea why?

     Folder folder = store.getDefaultFolder();
     folder = folder.getFolder("INBOX");
    
     int totalMessages = folder.getMessageCount();
     //totalMessages is 17000
    
      folder.getMessages(16900, 16999)   --- here I am trying to get the NEWEST 99                                                                                         messages.
    

This code throws the exception indexoutofbounds even though there are so many mails. What am I doing wrong?

  1. folder.getMessages() - get all the emails from 1st email to last. In my case 17000!! How do I get the emails starting from newest to oldest? I only want to see the latest emails - around 100 of them. Is this possible?

Upvotes: 4

Views: 2139

Answers (2)

James
James

Reputation: 1001

please open the folder in read or read_write mode first.

folder.open(Folder.READ_WRITE);

Upvotes: 0

mark-cs
mark-cs

Reputation: 4707

Never us magic values in code, it will just cause you pain. Try:

int messagesToDisplay = 100;
folder.getMessages(totalMessages  - messagesToDisplay , totalMessages);

From the JavaDoc the messages are identified with a 1 based array not 0.

Can you also add the stack trace please.

Upvotes: 5

Related Questions