Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Create a custom Attachment Handler on the BlackBerry device

by Retired ‎02-16-2010 11:57 AM - edited ‎02-17-2010 03:41 PM (2,754 Views)


This article applies to BlackBerry® devices based on Java® technology.


Some file attachment types can be viewed on a BlackBerry device using the BlackBerry Attachment Service on the BlackBerry® Enterprise Server or the BlackBerry® Internet Service.

You can create an application to have the BlackBerry device process a specific attachment type, or deliver data to a BlackBerry device application as an email attachment.

By default, email attachments are not delivered or stored on the BlackBerry device. To force an email attachment to be sent and stored on the BlackBerry device, prefix the attachment name with x-rimdevice.

Note: S/MIME (Secure Multipurpose Internet Mail Extensions) attachments are not delivered to the BlackBerry device, regardless of the x-rimdevice prefix.

The following code sample shows how to create an Attachment Handler on the BlackBerry device. This application adds a menu option of View File to the email display screen menu if the message contains a Wireless Markup Language (WML) file attachment.

* My Attachment Viewer

package com.samples.attachmentViewer;

import net.rim.blackberry.api.mail.*;
import net.rim.blackberry.api.mail.event.*;
import net.rim.device.api.system.*;

class MyViewer extends Application implements AttachmentHandler

public static void main(String args[])
MyViewer theApp = new MyViewer();

public MyViewer()
//Add this AttachmentHandler to the AttachmentHandlerManager
AttachmentHandlerManager m = AttachmentHandlerManager.getInstance();

//Called when a new email message arrives to see if it is supported
//by this AttachmentHandler (this sample supports wml).
//If it is true then the menu item will be shown in the message screen.
public boolean supports(String contentType)

boolean val;

if (contentType.toLowerCase().indexOf("wml") != -1)
val = true;
val = false;

return val;

//Then menu item that will appear from the message screen
public String menuString()
return "View File";

//Runs when the user selects the above defined menu item
public void run(Message m, SupportedAttachmentPart p)
//Perform required processing on your attachment here.
//Get the attachment filename.
System.out.println("WML filename: " + p.getFilename());
//Get the attachment size.
System.out.println("Attachment size is: " + p.getSize());
//Get the contents of the attachment.
byte[] temp = (byte[])p.getContent();
String data = new String(temp);
System.out.println("Attachment Content: " + data);


Users Online
Currently online: 22 members 2,956 guests
Please welcome our newest community members: