Work with message folders

  1. Import the required classes and interfaces.
    import net.rim.blackberry.api.invoke.Invoke;
    import net.rim.blackberry.api.invoke.MessageArguments;
    import net.rim.blackberry.api.mail.Folder;
    import net.rim.blackberry.api.mail.FolderNotFoundException;
    import net.rim.blackberry.api.mail.Message;
    import net.rim.blackberry.api.mail.Session;
    import net.rim.blackberry.api.mail.Store;
    
  2. Retrieve the store.
    Store store = Session.waitForDefaultSession().getStore();
  3. Complete any of the following actions:

    Task

    Steps

    Open a folder view

    1. Retrieve a list of folders.
      Store store = null;
      store = 
        Session.waitForDefaultSession().getStore();
      Folder[] folders = store.list();
      
    2. Invoke Invoke.invokeApplication() to view a folder from the list.
      Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES, 
      new MessageArguments( folders[0]));

    List the folders in a mailbox store

    1. Invoke Store.list().
      Folder[] folders = store.list();

    Retrieve an array of folders by type

    1. Invoke Store.list(int) and provide as a parameter the folder type.
      Folder[] folders = store.list(INBOX);
      Folder inbox = folders[0];
      

    Retrieve an array of folders through a search

    1. Invoke Store.findFolder(String).
      Folder[] folders = store.findFolder("Inbox");

    Retrieve a folder by its name

    1. Invoke Store.getFolder(String) and provide as a parameter the absolute path to the folder.
      Folder folder = 
        store.getFolder("Mailbox - Yan Wang/Inbox/Projects");
    2. Create code to manage a FolderNotFoundException exception if the folder does not exist.

    Retrieve a folder by its ID

    1. Invoke Folder.getID() to retrieve the folder ID.
    2. Invoke Store.getFolder() with the ID as a parameter.
      Folder[] folders = store.list();
      long id = folders[0].getId();
      Folder f2 = store.getFolder(id);
      

    File a message

    1. Invoke Folder.appendMessage(Message) on a Folder object.
      Message msg = new Message();
      // populate the message
      Folder folder = store.getFolder("Inbox");
      folder.appendMessage(msg);
      
Next topic: Custom messages
Previous topic: Forward a message

Was this information helpful? Send us your comments.