How to Download the Attachments from Mail Client in BlackBerry


Here i am trying to download the attachment from the E-Mails of the Mail Client. For this, i used the AttachmentHandler API. But using AttachmentHandler API, the three big shortcomings are there:

  1. Attachment name should be prefixed with x-rimdevice
  2. Can download the attachment only from the Messages Application of the phone.
  3. It will return the content type  if the attachment type is not supported by BlackBerry phone like .html or .pdf.

To avoid this shortcomings, i have tried the below code which is really helpful to get the content of the attachment(For me it is html file) and if require, you can parse it also. It will work for Messages and for the other configured Mail client also. As my requirement, the app should start on the start up of the device and the app icon should not display. For this, in descriptor file, i checked Auto-run on start-up  and Do not display the application icon on the BlackBerry Home Screen option.

So please check and use the below code:

public class MyApp extends UiApplication {

private AttachmentDownloadManager mDownloadManager;
 private Hashtable tableValues;
 private String mEncData;
 private String mUserName;
 private String mOrgName;
 private String mKeyVersion;
 private String mEPayload;
 private String mSendURL;
 private String mId;
 private String mChallenge;
 private String mAuthToken;
 private String mPin;
 BodyPart bp = null;
 private Store store;
 static MyApp theApp = null;

public static void main(String args[]) {
 try {
 theApp = new MyApp();
 } catch (Exception e) {
 Dialog.alert("Exception e = " + e.toString());
 } finally {
 Logger.out("MyViewer", "Args Length :" + args.length);

private MyApp() {
 Add the menu item for the Mail client. It will add for every mail client.
 ApplicationMenuItemRepository amir = ApplicationMenuItemRepository.getInstance();
 ApplicationDescriptor app = ApplicationDescriptor.currentApplicationDescriptor();
 SampleMenuItem mi = new SampleMenuItem(app);
 amir.addMenuItem(ApplicationMenuItemRepository.MENUITEM_EMAIL_VIEW, mi);

private class SampleMenuItem extends ApplicationMenuItem {

Message msg;
 ApplicationDescriptor app;

SampleMenuItem(ApplicationDescriptor app) {
 super(20); = app;

public String toString() {
 return "Open The attachment";

public Object run(Object context) {
 if (context instanceof Message) {
 msg = (Message) context;
 try {
 } catch (Exception e) {
 // TODO Auto-generated method stub
 return null;
 return context;


private void downloadAttchment(Message message) {
 mDownloadManager = new AttachmentDownloadManager();
 Multipart multi = (Multipart) message.getContent();
 for (int i = 0; i < multi.getCount(); i++) {
 BodyPart bp = multi.getBodyPart(i);
 //Checking whether the attachment is supported attachment or not
 if ((bp instanceof SupportedAttachmentPart) && (bp.hasMore())) {
 mDownloadManager = new AttachmentDownloadManager();
 String type = mDownloadManager.getFileContentType(bp);
 // checking whether the type of the attachment is html file or not
 if (type.trim().endsWith("htm") || type.trim().endsWith("html")) {
 try {, null, new DownloadProgressListener() {

public void downloadCancelled(Object element) {
 // TODO Auto-generated method stub


public void downloadCompleted(Object element) {
 // TODO Auto-generated method stub
 BodyPart bpnew = (BodyPart) element;
 startParse(bpnew); // Parse the html file here


public void updateProgress(Object element, int current, int total) {
 // TODO Auto-generated method stub


 } catch (IOException e) {
 // TODO Auto-generated catch block



Hope it will help.



One thought on “How to Download the Attachments from Mail Client in BlackBerry

  1. I would like to ask you some question regarding ksoap2 and Android. I have a similar problem as you had on stackoverflow. (

    I am very new to this and am facing problem of consuming such an object in Android. If you could share your previous code, it will be very helpful to learn. I have looked around and have not found any useful tutorials.

    My email is

    Thank You.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s