Changes

Jump to: navigation, search

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

1,841 bytes added, 03:13, 23 March 2011
Created page with '== Adding Send Email Functionality == 9.1 Lets first add the api which we will need for the email functionality <pre> import net.rim.blackberry.api.mail; </pre> 9.2 In order …'
== Adding Send Email Functionality ==

9.1 Lets first add the api which we will need for the email functionality
<pre>
import net.rim.blackberry.api.mail;
</pre>

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

<pre>
// 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

}
}
));

</pre>

9.3 Now lets get the students email address and name
<pre>
Student student = (Student) _keywordFilterField.getSelectedElement();
String studentEmail = student.getEmail();
String studentName = student.getname();
</pre>

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
<pre>
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);
</pre>

9.5 Lets catch any exceptions and notify the user if the message was sent.
<pre>
catch (Exception me) {
System.err.println(me);
}

Dialog.alert(studentName + " has been emailed!");
</pre>
1
edit

Navigation menu