Open a message

  1. Import the required classes and interfaces.
    import java.util.Date;
    import net.rim.blackberry.api.mail.Address;
    import net.rim.blackberry.api.mail.Folder;
    import net.rim.blackberry.api.mail.Message;
    import net.rim.blackberry.api.mail.Session;
    import net.rim.blackberry.api.mail.Store;
    
  2. Invoke Session.waitForDefaultSession.getStore() to retrieve the message store.
    Store store = Session.waitForDefaultSession.getStore();
    
  3. Invoke Store.getFolder() to retrieve the folder that contains the message.
    Folder folder = Store.getFolder("SampleFolder");
    
  4. Invoke folder.getMessages() to retrieve the message objects and store the message objects in a Message array. Iterate through the array and retrieve information, such as the sender and subject, to display to the BlackBerry® device user.
    Message[] msgs = folder.getMessages();
  5. When a user selects a message from the list, invoke methods on the Message object to retrieve the appropriate fields and body contents to display to the user.
    Message msg = msgs[0]; // Retrieve the first message
    Address[] recipients = msg.getRecipients(Message.RecipientType.TO);
    Date sent = msg.getSentDate();
    Address from = msg.getFrom();
    String subject = msg.getSubject();
    Object o = msg.getContent();
    
    // Verify that the message is not multipart
    if ( o instanceof String ) 
    {
       String body = (String)o;
    }
    
    //...
    
  6. Invoke Message.getBodyText() on a message to retrieve the plain text contents as a String. If the message does not contain plain text, the method returns null.

Was this information helpful? Send us your comments.