07-31-2008 01:55 PM
Hi,
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:-
try
{
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.
07-31-2008 02:18 PM
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
07-31-2008 02:41 PM
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:
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] ); } } } }