Teams Winter 2011/team1/BlackBerry/Add Send Email Option

From CDOT Wiki
Revision as of 12:31, 28 March 2011 by Azea (talk | contribs)
Jump to: navigation, search

Adding Send Email Functionality

9.1 Lets first add the api which we will need for the email functionality

  import net.rim.blackberry.api.mail;

9.2 In order to add send email functionality to our application, we must have the appropriate menu option in our application.

// Email selected student
MenuItem emailStudent = new MenuItem(new StringProvider("Email Student"), 500, 5);
editItem.setCommand(new Command(new CommandHandler(){

public void execute(ReadOnlyCommandMetadata metadata, Object context) {
// TODO Auto-generated method stub

}
}
));

9.3 Now lets get the students email address and name

  Student student = (Student) _keywordFilterField.getSelectedElement();
  String studentEmail = student.getEmail();
  String studentName = student.getname();

9.4 Now we must set up a new message and content. Here we create a new message in the SENT folder of the device and fill it with a predefined body

  Store store = Session.getDefaultInstance().getStore();
  Folder sentFolder = store.list(Folder.SENT);
  Message msg = new Message(sentFolder);
                   
                      
  // Set up recipients
  Address recipients = new Address(studentEmail, studentName);
  //Add the recipient to the message
  msg.addRecipients(Message.RecipientType.TO, recipients);
  //set a subject for the message
  msg.setSubject(“Team 1 test email”);
  //sets the body of the message
  msg.setContent(“This is a test email sent to: ” + studentName);
  //sets priority
  msg.setPriority(Message.Priority.HIGH);
  //send the message
  Transport.send(msg);

9.5 Lets catch any exceptions and notify the user if the message was sent.

  catch (Exception me) {
    System.err.println(me);
  }
                 
  Dialog.alert(studentName + " has been emailed!");