Send a message from a non-default email address

by BlackBerry Development Advisor ‎02-16-2010 01:14 PM - edited ‎02-17-2010 03:57 PM (3,488 Views)

Summary


This article applies to the following:

  • BlackBerry® Device Software 4.0 and later
  • BlackBerry Java® Development Environment (JDE) 4.0 and later


Description


If a BlackBerry device user has multiple email accounts integrated with a BlackBerry device, an application may want to send a message from the BlackBerry device using a non-default email address. The following sample shows how to send a message from each email account associated with the BlackBerry device.



ServiceBook sb = ServiceBook.getSB();
ServiceRecord[] srs = sb.findRecordsByCid("CMIME");
if (srs != null) {
for (int i = srs.length -1; i >= 0; --i) {
ServiceRecord sr = srs[i];
try {
ServiceConfiguration sc = new ServiceConfiguration(sr);
Session session = Session.getInstance(sc);
Store store = session.getStore();
Folder[] folders = store.list(Folder.SENT);
Folder sentfolder = folders[0];
//Create a new message and associate it with
//the sent folder of the email account. The “From” field of the
//new message will also be populated with the associated
//email account address.
Message msg = new Message(sentfolder);
Address[] addrs = new Address[1];
addrs[0] = new Address("joe@yahoo.com", "Joe");
msg.addRecipients(Message.RecipientType.TO, addrs);
msg.setSubject("Hello");
msg.setContent("Hi there!");
Transport.send(msg);
}
catch (Exception e) {}
}
}