Hi,
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:
- Attachment name should be prefixed with x-rimdevice
- Can download the attachment only from the Messages Application of the phone.
- 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:
</pre>
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 {
theApp.enterEventDispatcher();
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);
this.app = app;
}
public String toString() {
return "Open The attachment";
}
public Object run(Object context) {
if (context instanceof Message) {
msg = (Message) context;
try {
downloadAttchment(msg);
} catch (Exception e) {
e.printStackTrace();
}
// 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 {
mDownloadManager.download(bp, 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
e.printStackTrace();
}
}
}
}
}
}
<pre>
Hope it will help.



