Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

New Developer
Posts: 40
Registered: ‎07-14-2008
My Device: Not Specified

Reading mail messages that are already in the inbox



I need to read email messages that are already in the inbox to check for a particular message as well as listen to new messages arriving (the confirmation email I send may have already arrived before the user launches or even installs my app). I know how to set up a listener for new messages from reading the forums ( Mail Listener ), but I can't figure out how to search through existing messages.


I want to do something along the lines of:- 


            Store store = Session.getDefaultInstance().getStore();
            net.rim.blackberry.api.mail.Message [] msgs = store.getMessages();
            for(int i=0; i < msgs.length; i++)
                if (msgs[i].isInbound())
                    String subject = msgs[i].getSubject();
                    if ((subject != null) && (subject.equals("My subject")))
                      //do stuff


But store.getMessages() isnt valid on a store and I cant see any other api call I can use to get at the messages that are already in the store.

BlackBerry Development Advisor
Posts: 15,882
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: Reading mail messages that are already in the inbox

Please have a look at the FolderListScreen found in the complete example you can download using the link below.  Please note that the full example requires BlackBerry JDE version 4.5.0+.


How To - Access HTML email messages
Article Number: DB-00666


Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Posts: 120
Registered: ‎07-16-2008
My Device: 9000

Re: Reading mail messages that are already in the inbox

Here is some sample code of how to iterate through existing messages.  It uses a couple of our library classes but it shouldn't be much work to refactor those out.  Some things to note are that:

  1. It uses service books to get all email accounts.
  2. It checks the sub-folders because some BES accounts have sub folders in their inbox
  3. You can check your message where the comment says: Check the message here


import net.rim.blackberry.api.mail.Folder; import net.rim.blackberry.api.mail.Message; import net.rim.blackberry.api.mail.MessagingException; import net.rim.blackberry.api.mail.ServiceConfiguration; import net.rim.blackberry.api.mail.Session; import net.rim.blackberry.api.mail.Store; import net.rim.device.api.servicebook.ServiceBook; import net.rim.device.api.servicebook.ServiceRecord; public class EmailScanner { public void scanEmail() throws MessagingException { Set uniqueStores = getUniqueStores(); Iterator iterator = uniqueStores.iterator(); while (iterator.hasNext()) { Store store = (Store) iterator.next(); Folder[] folders = store.list(); for (int i = 0; i < folders.length; i++) { Folder folder = folders[i]; checkFolder( folder ); } } } private Set getUniqueStores() { Set uniqueStores = new HashSet(); Session defaultSession = Session.getDefaultInstance(); if (defaultSession != null) { uniqueStores.add( defaultSession.getStore() ); } ServiceBook serviceBook = ServiceBook.getSB(); ServiceRecord[] serviceRecords = serviceBook.findRecordsByCid( "CMIME" ); for (int i = 0; i < serviceRecords.length; i++) { ServiceConfiguration serviceConfiguration = new ServiceConfiguration( serviceRecords[i] ); Store store = Session.getInstance( serviceConfiguration ).getStore(); if (!uniqueStores.contains( store )) { uniqueStores.add( store ); } } return uniqueStores; } private void checkFolder( Folder folder ) throws MessagingException { if (folder.getType() == Folder.OUTBOX) { log.debug( "Skiping folder " + folder.getFullName() + " because it is the outbox." ); } else { log.debug( "Checking messages in folder " + folder.getFullName() ); Message[] messages = folder.getMessages(); for (int j = messages.length - 1; j >= 0; j--) { Message message = messages[j]; //Check the message here. } Folder[] subFolders = folder.list(); for (int i = 0; i < subFolders.length; i++) { //Recusive call to check subfolders checkFolder( subFolders[i] ); } } } }



Kurt Zettel | Chief Architect | Metova