Sorry about the red box, but we really need you to update your browser. Read this excellent article if you're wondering why we are no longer supporting this browser version. Go to Browse Happy for browser suggestions and how to update.

Message list

This section describes how to use the messaging capabilities of the BlackBerry device.

For more information, see the Messaging category overview in the API reference for the BlackBerry Java Development Environment.

Create a new blank SMS text message

Import the required classes and interfaces.

import net.rim.blackberry.api.invoke.Invoke;
import net.rim.blackberry.api.invoke.MessageArguments;

Invoke Invoke.invokeApplication(). Use the following parameters: the APP_TYPE_MESSAGES constant parameter and a new MessageArguments object that uses the ARG_NEW_SMS field.

Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES, 
   new MessageArguments( MessageArguments.ARG_NEW_SMS));

Create a new populated text message

Use the API items in the javax.wireless.messaging package (JSR 120).

Import the required classes and interfaces.

import javax.microedition.io.Connector;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;
import net.rim.blackberry.api.invoke.Invoke;
import net.rim.blackberry.api.invoke.MessageArguments;

Create and populate a new TextMessage object.

MessageConnection mc = (MessageConnection)Connector.open("sms://");
TextMessage m = (TextMessage)mc.newMessage( MessageConnection.TEXT_MESSAGE );
m.setAddress("sms://5558888");
m.setPayloadText("An SMS message for you");

Invoke Invoke.invokeApplication() with the following parameters:

  • APP_TYPE_MESSAGES: a constant parameter .
  • MessageArguments: a new MessageArguments object that uses the new TextMessage object.
Invoke.invokeApplication( Invoke.APP_TYPE_MESSAGES, 
new MessageArguments(m) );

Create a new blank MMS message

Import the required classes and interfaces.

import net.rim.blackberry.api.invoke.Invoke;
import net.rim.blackberry.api.invoke.MessageArguments;

Invoke Invoke.invokeApplication() using the APP_TYPE_MESSAGES constant parameter and a new MessageArguments object that uses the ARG_NEW_MMS field.

Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES, 
   new MessageArguments( MessageArguments.ARG_NEW_MMS));

Create a new blank email message

Import the required classes and interfaces.

import net.rim.blackberry.api.invoke.Invoke;
import net.rim.blackberry.api.invoke.MessageArguments;

Invoke Invoke.invokeApplication() using the APP_TYPE_MESSAGES constant parameter and a new MessageArguments object that uses the ARG_NEW field.

Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES, 
   new MessageArguments( MessageArguments.ARG_NEW));

Create a new populated email message

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.Address;
import net.rim.blackberry.api.mail.Message;

Create and populate a new email Message object.

Message m = new Message();
Address a = new Address("mLi@blackberry.com", "Ming Li");
Address[] addresses = {a};
m.addRecipients(net.rim.blackberry.api.mail.Message.RecipientType.TO, addresses);
m.setContent("A message for you...");
m.setSubject("Email for you");

Invoke Invoke.invokeApplication()with the following parameters:

  • APP_TYPE_MESSAGES.
  • a MessageArguments object that uses the new Message object.
Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES, 
new MessageArguments(m));

Create a new blank PIN message

Import the required classes and interfaces.

import net.rim.blackberry.api.invoke.Invoke;
import net.rim.blackberry.api.invoke.MessageArguments;

Invoke Invoke.invokeApplication() using the APP_TYPE_MESSAGES constant parameter and a new MessageArguments object that uses the ARG_NEW_PIN field.

Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES, 
   new MessageArguments( MessageArguments.ARG_NEW_PIN));

Create a new populated PIN message

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.Address;
import net.rim.blackberry.api.mail.Message;
import net.rim.blackberry.api.mail.PINAddress;

Create and populate a new PIN message.

Message m = new Message();
PINAddress pa = new PINAddress("ABCDEF99", "Mark Chapters");
Address[] addresses = {pa};
m.addRecipients( net.rim.blackberry.api.mail.Message.RecipientType.TO, 
   addresses );
m.setContent("A message for you...");
m.setSubject("PIN message for you");

Invoke Invoke.invokeApplication() with the following parameters:

  • APP_TYPE_MESSAGES
  • a MessageArguments object that uses the new PIN message.
Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES, 
new MessageArguments(m));

Receive a message notification

Import the required classes and interfaces.

import net.rim.blackberry.api.mail.event.FolderListener;
import net.rim.blackberry.api.mail.event.StoreListener;
import net.rim.device.api.system.ControlledAccessException;

Implement the FolderListener and StoreListener interfaces.

public class MailTest implements FolderListener, StoreListener { ... }

Check for a ControlledAccessException if your application accesses an object that you do not have permission to access.

Add a listener to the message store

Import the required classes and interfaces.

import net.rim.blackberry.api.mail.NoSuchServiceException;
import net.rim.blackberry.api.mail.Session;
import net.rim.blackberry.api.mail.Store;

Create a try-catch block to manage a NoSuchServiceException.

try 
{
   
} 
catch (NoSuchServiceException e) 
{
   System.out.println(e.toString());
}

Within the try-catch block, invoke Session.waitForDefaultSession().getStore() to retrieve the Store object.

try 
{
   Store store = Session.waitForDefaultSession().getStore();
} 
catch (NoSuchServiceException e) 
{
   System.out.println(e.toString());
}

After the try-catch block, invoke store.addStoreListener() to add a StoreListener instance to the Store object.

store.addStoreListener(this);

Add a listener to the message store for batch updates

Import the required classes and interfaces.

import net.rim.blackberry.api.mail.event.StoreListener;
import net.rim.blackberry.api.mail.Store;

Implement the StoreListener interface.

void batchOperation(StoreEvent e) 
{
   // Perform action when messages added or removed in batch operation.
}

Add a listener to a folder

Import the required classes and interfaces.

import net.rim.blackberry.api.mail.event.FolderListener;
import net.rim.blackberry.api.mail.Folder;
import net.rim.blackberry.api.mail.Store;

Implement FolderListener.messagesAdded() and FolderListener.messagesRemoved().

void messagesAdded(FolderEvent e) 
{
   // Perform processing on added messages.
}
void messagesRemoved(FolderEvent e) 
{
   // Perform processing on removed messages.
}

Retrieve the Folder object for which you want to receive new message notifications.

Folder[] folders = store.list(Folder.INBOX);
Folder inbox = folders[0];

Register the class that implements FolderListener with the folder.

inbox.addFolderListener(this);

Retrieve the total count of unread email messages in all folders in the store

Import the required class.

import net.rim.blackberry.api.mail.Store;

Invoke Store.getUnreadMessageCount().

int numUnread = store.getUnreadMessageCount();

Open a message

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;

Invoke Session.waitForDefaultSession.getStore() to retrieve the message store.

Store store = Session.waitForDefaultSession.getStore();

Invoke Store.getFolder() to retrieve the folder that contains the message.

Folder folder = Store.getFolder("SampleFolder");

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();

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;
}

//...

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.

Retrieving the body of an email message

An email message can contain plain text, HTML, or both. The content and the order of the content in the email message can vary.

A BlackBerry device application can use the MimeBodyPart class to retrieve the HTML content, or use the TextBodyPart class to retrieve the plain text content. You can use a MultiPart object to retrieve objects from both the MimeBodyPart class and the TextBodyPart class.

For example, you might retrieve the content of an email message to translate the content into a different language.

Support for text and HTML was introduced in BlackBerry Device Software 4.5 for BlackBerry devices associated with BlackBerry Enterprise Server 4.1 Service Pack 6 (4.1.6) or the BlackBerry Internet Service 2.5.

Retrieve the plain text and HTML content in the body of an email message using a recursive method

Import the required classes and interfaces.

import net.rim.blackberry.api.mail.MimeBodyPart;
import net.rim.blackberry.api.mail.Multipart;
import net.rim.blackberry.api.mail.SupportedAttachmentPart;
import net.rim.blackberry.api.mail.TextBodyPart;
import net.rim.blackberry.api.mail.UnsupportedAttachmentPart;

Create the method signature for the recursive method.

void findEmailBody(Object obj) {...}

Create local variables that indicate if the BlackBerry Attachment Service supports the message attachment type.

boolean _hasSupportedAttachment; 
boolean _hasUnsupportedAttachment;

Initialize the local variables.

_hasSupportedAttachment = false; 
_hasUnsupportedAttachment = false;

If the method parameter is a Multipart object, the object has multiple BodyPart objects. On each BodyPart object, invoke the recursive method that searches through the body of an email message.

if(obj instanceof Multipart)
{ 
   _Multipart mp = (Multipart)obj;

   //Extract all of the parts within the Multipart message.
   for(int count=0; count < mp.getCount(); ++count)
   { 
      findEmailBody(mp.getBodyPart(count));
   }
}

If the BodyPart object is a TextBodyPart, retrieve the plain text body of the message.

else if (obj instanceof TextBodyPart)
{
   //This message only has a text body.
   TextBodyPart tbp = (TextBodyPart) obj; 
   readEmailBody(tbp);
}

Check if the BodyPart object is a MimeBodyPart.

else if (obj instanceof MimeBodyPart) {...}

If the BodyPart object is a MimeBodyPart, perform the following actions:

  • Cast the BodyPart object as a MimeBodyPart
    MimeBodyPart mbp = (MimeBodyPart) obj;  
    
  • If the MimeBodyPart object does not contain attachments, retrieve the body of the message using the MimeBodyPart object as a parameter.
    if (mbp.getContentType().indexOf(ContentType.TYPE_TEXT_HTML_STRING) != -1)
    {
       readEmailBody(mbp);
    }
    
  • If the MimeBodyPart object does contain attachments, invoke a method that retrieves the body of the message.
    else if 
       (mbp.getContentType().equals(ContentType.TYPE_MULTIPART_MIXED_STRING) || 
          mbp.getContentType().equals(ContentType.TYPE_MULTIPART_ALTERNATIVE_STRING))
    {
       findEmailBody(mbp.getContent());
    }
    

If the BodyPart is an attachment that the BlackBerry Attachment Service supports, change the appropriate local variable to true.

else if (obj instanceof SupportedAttachmentPart)
{
   _hasSupportedAttachment = true;
} 

If the BodyPart is an attachment that the BlackBerry Attachment Service does not support, change the appropriate local variable to true.

else if (obj instanceof UnsupportedAttachmentPart)
{
   _hasUnsupportedAttachment = true;
} 

Retrieving the content of an email message

private void findEmailBody(Object obj)
{
   //Reset the attachment flags.
   _hasSupportedAttachment = false;
   _hasUnsupportedAttachment = false;

   if(obj instanceof Multipart)
   {
      Multipart mp = (Multipart)obj;
    
      for(int count=0; count < mp.getCount(); ++count)
      {
        findEmailBody(mp.getBodyPart(count));
      }
   }
    
   else if (obj instanceof TextBodyPart)
   {
      TextBodyPart tbp = (TextBodyPart) obj;
      readEmailBody(tbp);
   }

   else if (obj instanceof MimeBodyPart)
   {
      MimeBodyPart mbp = (MimeBodyPart)obj;
      if (mbp.getContentType().indexOf(ContentType.TYPE_TEXT_HTML_STRING) != -1)
      {
        readEmailBody(mbp);
      }
   }

   else if (mbp.getContentType().equals(ContentType.TYPE_MULTIPART_MIXED_STRING) ||
   mbp.getContentType().equals(ContentType.TYPE_MULTIPART_ALTERNATIVE_STRING))
   {    
      //The message has attachments or we are at the top level of the message.
      //Extract all of the parts within the MimeBodyPart message.
      findEmailBody(mbp.getContent());
   }
   
   else if (obj instanceof SupportedAttachmentPart)  
   {
      _hasSupportedAttachment = true;
   }

   else if (obj instanceof UnsupportedAttachmentPart) 
   {
      _hasUnsupportedAttachment = true;
   }
}

Retrieve the plain text content of an email message

In the following task, an exception may be thrown when you invoke the Transport.more() method.

//Create a method that takes a TextBodyPart object as a parameter.
private void readEmailBody(TextBodyPart tbp)
{

//Cast the value that TextBodyPart.getContent() returns as a String 
//to get the plain text  part of the body of the message.
   _plainTextMessage = (String)tbp.getContent();

//Invoke TextBodyPart.hasMore() and TextBodyPart.moreRequestSent() 
//to identify if more of the TextBodyPart object is available on the server.
		
   if (tbp.hasMore() && !tbp.moreRequestSent())
   {
      try
      {

//If more data is available for the TextBodyPart object, invoke
//Transport.more()  to retrieve the rest of the TextBodyPart object.
         Transport.more((BodyPart)tbp, true);
      }
      catch (Exception ex)
      {
         Dialog.alert("Exception: " + ex.toString());
      }
   }
}   

Retrieve the HTML content of an email message

In the following task, an exception may be thrown when you invoke the Transport.more() method.

//Create a method that takes a MimeBodyPart object as a parameter.
private void readEmailBody(MimeBodyPart mbp)
{

//Invoke MimeBodyPart.getContent() and MimeBodyPart.getContentType() 
//to retrieve the content of the MimeBodyPart object.
   Object obj = mbp.getContent();
   String mimeType = mbp.getContentType();

//Create a String variable to hold the String representation of the 
//MimeBodyPart object.
   String body = null;

//If the BlackBerry device can convert the HTML body of 
//a message to a String, the MimeBodyPart object will be a String. 
//Cast the MimeBodyPart object as a String and assign it to the 
//String representation of the body part of  the message.
   if (obj instanceof String)
   {
      body = (String)body;
   }

//If the BlackBerry device cannot convert the HTML body 
//of a message to a String, the MimeBodyPart object will be  a byte array. 
//Create a new instance of String using as a parameter the MimeBodyPart 
//object cast as a byte array. Assign the String to the String 
//representation of the body part of the message.
   else if (obj instanceof byte[])
   {
      body = new String((byte[])obj);
   }

//Check to see if the String representation of the content of the 
//MimeBodyPart object contains ContentType.TYPE_TEXT_PLAIN_STRING 
//to determine if the MimeBodyPart object is the plain text body 
//part of the message.
   if (mimeType.indexOf(ContentType.TYPE_TEXT_PLAIN_STRING) != -1)
   {
      _plainTextMessage = body;

//Invoke MimeBodyPart.hasMore() and MimeBodyPart.moreRequestSent() 
//to determine if all of the text body part is present in the 
//MimeBodyPart object.
      if (mbp.hasMore() && !mbp.moreRequestSent())
      {
         try
         {

//If more data is available for the MimeBodyPart object, invoke 
//Transport.more()  to retrieve the rest of the data for the MimeBodyPart object.
            Transport.more((BodyPart)mbp, true);
         }
         catch (Exception ex)
         {
            Dialog.alert("Exception: " + ex.toString());
         }
      }
   }
  
//Check to see if the String representation of the content of the 
//MimeBodyPart object contains ContentType.TYPE_TEXT_HTML_STRING 
//to detemine if the MimeBodyPart object is the HTML body part of the message.
   else if (mimeType.indexOf(ContentType.TYPE_TEXT_HTML_STRING) != -1)
   {
      _htmlMessage = body;

//Invoke MimeBodyPart.hasMore() and MimeBodyPart.moreRequestSent() 
//to determine if all of the HTML body part is present in the 
//MimeBodyPart object.
      if (mbp.hasMore() && !mbp.moreRequestSent())
      {
         try
         {

//If more data is available for the MimeBodyPart object, 
//invoke Transport.more()  to retrieve the rest of the MimeBodyPart object.
            Transport.more((BodyPart)mbp, true);
         }
         catch (Exception ex)
         { 
            Dialog.alert("Exception: " + ex.toString());
         }
      }
   }
}

Notify a BlackBerry device application that an email message is about to be sent

Import the required classes and interfaces.

import net.rim.blackberry.api.mail.NoSuchServiceException;
import net.rim.blackberry.api.mail.SendListener;
import net.rim.blackberry.api.mail.Session;
import net.rim.blackberry.api.mail.Store;

Implement the SendListener interface.

public class MailSendListener implements SendListener{...}

Create an instance of the class that implements the SendListener interface.

MailSendListener mailSL = new mailSendListener();

In a try block, invoke Session.waitForDefaultSession().getStore() to retrieve the Store object.

try 
{
   Store store = Session.waitForDefaultSession().getStore();
}

In a catch block, manage a NoSuchServiceException.

catch (NoSuchServiceException e) 
{
   System.out.println(e.toString());
}

Invoke Store.addSendListener(MailSendListener) to add a SendListener instance.

store.addSendListener(mailSL);

Notify a BlackBerry device application that an MMS message is about to be sent

Import the required classes and interfaces.

import net.rim.blackberry.api.mms.SendListener;
import net.rim.blackberry.api.mms.MMS;

Implement the SendListener interface.

public class MMSSendListener implements SendListener{...}

Create an instance of the class that implements the SendListener interface.

MMSSendListener mmsSL = new mmsSendListener();

Add a SendListener instance.

MMS.addSendListener(mmsSL);

Notify a BlackBerry device application that an SMS message is about to be sent

Import the required classes and interfaces.

import net.rim.blackberry.api.sms.SendListener;
import net.rim.blackberry.api.sms.SMS;

Create a class that implements the SendListener interface.

public class smsSendListener implements SendListener{...}

Create an instance of the class that implements the SendListener interface.

smsSendListener smsSL = new smsSendListener();

Add a SendListener.

SMS.addSendListener(smsSL);

Send a message

Import the required classes and interfaces.

import net.rim.blackberry.api.mail.Address;
import net.rim.blackberry.api.mail.AddressException;
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.Session;
import net.rim.blackberry.api.mail.Store;
import net.rim.blackberry.api.mail.Transport;

Declare a Message object.

Message msg;

Specify a folder in which to save a copy of the sent message.

Store store = Session.getDefaultInstance().getStore();
Folder[] folders = store.list(Folder.SENT);
Folder sentfolder = folders[0];
msg = new Message(sentfolder);

Create an array of Address objects.

Address toList[] = new Address[1];

In a try block, add each address to the array.

try 
{
   toList[0]= new Address("ming.li@example.com", "Ming Li");
}

In a catch block, manage a AddressException, which is thrown if an address is invalid.

catch(AddressException e) 
{
   System.out.println(e.toString());
}

Invoke Message.addRecipients() and provide the type of recipient (TO, CC, or BCC) and the array of addresses to add as parameters to the method.

If the message has multiple types of recipients, invoke Message.addRecipients() once for each recipient type.

msg.addRecipients(Message.RecipientType.TO, toList);

Invoke Message.setFrom(Address).

Address from = new Address("ming.li@example.com", "Ming Li");
msg.setFrom(from);

Invoke Message.setSubject(String).

msg.setSubject("Test Message");

Invoke Message.setContent(String). (Typically, the BlackBerry device application retrieves content from text that a BlackBerry device user types in a field.)

try 
{
   msg.setContent("This is a test message.");
} 
catch(MessagingException e) 
{
   System.out.println(e.getMessage());
}

Invoke Session.getTransport() and store the returned object in a variable of type Transport. The Transport object represents the messaging transport protocol.

Transport trans = Session.getTransport();

Invoke Transport.send(Message) to send the message.

try 
{
   trans.send(msg);
} 
catch(MessagingException e) 
{
   System.out.println(e.getMessage());
}

Reply to a message

Import the required classes and interfaces.

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;
import net.rim.blackberry.api.mail.Transport;

Invoke Session.getTransport() and store the returned object in a variable of type Transport. The Transport object represents the messaging transport protocol.

Transport trans = Session.getTransport();

Invoke Session.waitForDefaultSession().getStore() to retrieve the Store object.

Store store = Session.waitForDefaultSession().getStore();

Invoke Store.list(INBOX) to retrieve all the folders in the INBOX folder. Store the folders in a Folder array.

Folder[] folders = store.list(INBOX);

Specify a specific array element to retrieve the inbox folder.

Folder inbox = folders[0];

Invoke Folder.getMessages() to retrieve the messages in the inbox folder. Store the messages in a Message array.

Message[] messages = inbox.getMessages();

Invoke Message.reply(Boolean) and specify true to reply to all message recipients or false to reply to only the sender.

if( messages.length > 0 ) 
{
   Message msg = messages[0];
}
Message reply = msg.reply(true);

Invoke Transport.send(Message) to send the reply.

try 
{
   trans.send(reply);
} 
catch(MessagingException e) 
{
   System.out.println(e.getMessage());
}

Forward a message

Import the required classes and interfaces.

import net.rim.blackberry.api.mail.Address;
import net.rim.blackberry.api.mail.Message;
import net.rim.blackberry.api.mail.MessagingException;
import net.rim.blackberry.api.mail.Session;
import net.rim.blackberry.api.mail.Transport;

Invoke Message.forward() on an existing Message object. The subject line of a forwarded message is set automatically to FW:original_subject.

Message fwdmsg = msg.forward();

Create an array of addresses.

Address toList[] = new Address[1];

Add a new Address object to the array.

toList[0]= new Address("ming.li@example.com", "Ming Li");

Invoke Message.addRecipients(int, Address[]) to add recipients to the Message.

fwdmsg.addRecipients(Message.RecipientType.TO, toList);

Invoke Message,setContent(String) to set the content of the message that appears before the original message.

try 
{
   fwdmsg.setContent("This is a forwarded message.");
} 
catch(MessagingException e) 
{
   System.out.println(e.getMessage());
}

Invoke Session.getTransport() and store the returned object in a variable of type Transport. The Transport object represents the messaging transport protocol.

Transport trans = Session.getTransport();

Invoke Transport.send(Message).

try 
{
   trans.send(fwdmsg);
} 
catch(MessagingException e) 
{
   System.out.println(e.getMessage());
}

Work with message folders

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;

Retrieve the store.

Store store = Session.waitForDefaultSession().getStore();

Complete any of the following actions.

Open a folder view:
  • Retrieve a list of folders.
    Store store = null;
    store = 
      Session.waitForDefaultSession().getStore();
    Folder[] folders = store.list();
    
  • 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:
  • Invoke Store.list().
    Folder[] folders = store.list();
Retrieve an array of folders by type:
  • 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:
  • Invoke Store.findFolder(String).
    Folder[] folders = store.findFolder("Inbox");
Retrieve a folder by its name:
  • Invoke Store.getFolder(String) and provide as a parameter the absolute path to the folder.
    Folder folder = 
      store.getFolder("Mailbox - Yan Wang/Inbox/Projects");
  • Create code to manage a FolderNotFoundException exception if the folder does not exist.
Retrieve a folder by its ID:
  • Invoke Folder.getID() to retrieve the folder ID.
  • 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:
  • Invoke Folder.appendMessage(Message) on a Folder object.
    Message msg = new Message();
    // populate the message
    Folder folder = store.getFolder("Inbox");
    folder.appendMessage(msg);