* @return A new message
*/
Message getMessage() {
// Find an outbox folder and use it to construct a new message
final Folder outbox = _store.findFolder("Outbox")[FIRST];
final Message message = new Message(outbox);
// Add all the current headers
for (int keyNo = 0; keyNo < HEADER_KEYS.length; keyNo++) {
final Vector fieldsByType =
(Vector) _fieldTable.get(HEADER_KEYS[keyNo]);
if (fieldsByType != null) {
// Build a vector of all the addresses
final Vector addressVector = new Vector();
final int size = fieldsByType.size();
for (int fieldNo = 0; fieldNo < size; fieldNo++) {
final TextField addressField =
(TextField) fieldsByType.elementAt(fieldNo);
// Try to create a new address object wrapping the email
// address and add it to the address vector.
try {
addressVector.addElement(new Address(addressField
.getText(), ""));
} catch (final AddressException e) // Invalid address
{
BlackBerryMailDemo
.errorDialog("Address(String, String) threw "
+ e.toString());
}
}
// Dump the vector of addresses into an array to send the
// message
final Address[] addresses = new Address[addressVector.size()];
addressVector.copyInto(addresses);
// Try to add the addresses to the message's list of recipients
try {
message.addRecipients(HEADER_KEYS[keyNo], addresses);
} catch (final MessagingException e) {
BlackBerryMailDemo
.errorDialog("Message#addRecipients(int, Address[]) threw "
+ e.toString());
}
}
}
// Add the subject
final Vector subjectFields = (Vector) _fieldTable.get(SUBJECT);
final TextField subjectField =
(TextField) subjectFields.elementAt(FIRST);
if (subjectFields != null && subjectFields.size() > 0) {
message.setSubject(subjectField.getText());
}
// Add the body by adding all the body fields into one multipart
final Vector bodyFields = (Vector) _fieldTable.get(BODY);
if (bodyFields != null) {
final int size = bodyFields.size();
final Multipart content = new Multipart();
for (int fieldNo = 0; fieldNo < size; fieldNo++) {
final TextField body =
(TextField) bodyFields.elementAt(fieldNo);
content.addBodyPart(new TextBodyPart(content, body.getText()));
}
try {
message.setContent(content);
} catch (final MessagingException e) {
BlackBerryMailDemo
.errorDialog("Message#setContent(Object) threw "
+ e.toString());
}
} else {
BlackBerryMailDemo.errorDialog("Error: no body field available");
return null;
}
// Set the date
message.setSentDate(Calendar.getInstance().getTime());
return message;
}