04-03-2009 08:45 AM
Hi.
Looking for a way to retireve an xml attachment then automatically process the xml document and display it. I am stuck on how to retreive the xml attachment into a inputstream.
Here is what I have so far:
import java.io.InputStream; import org.w3c.dom.Document; import net.rim.blackberry.api.mail.Folder; import net.rim.blackberry.api.mail.Message; import net.rim.blackberry.api.mail.Multipart; import net.rim.blackberry.api.mail.event.FolderEvent; import net.rim.device.api.notification.NotificationsManag
er; import net.rim.device.api.system.RuntimeStore; import net.rim.device.api.ui.component.Dialog; import net.rim.device.api.xml.parsers.DocumentBuilder; import net.rim.device.api.xml.parsers.DocumentBuilderFact ory; public class test extends UiApplication implements FolderListener{ ... public void messagesAdded(FolderEvent e) { // Create a store to save the xml document into RuntimeStore store = RuntimeStore.getRuntimeStore(); long ATTACHMENT_ID = 0x227cdf69ca6b6ce8L; // ATTACHMENT_ID // get the message and folder it was added to Message msg = e.getMessage(); Folder messageFolder = msg.getFolder(); if (msg.getSubject().equalsIgnoreCase("XML TEST")) { Object content = msg.getContent(); if (content instanceof Multipart) { Multipart mp = (Multipart) content; for (int i = 0; i < mp.getCount(); i++) { try { // Build a document based on the XML file. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); // InputStream inputStream = // mp.getBodyPart(i).getInputStream(); Document document = builder.parse((InputStream) mp.getBodyPart(i).getContent()); store.put(ATTACHMENT_ID, document); } catch (Exception ex) { Dialog.alert("Error: " + ex.getMessage()); } } // delete the message messageFolder.deleteMessage(msg); NotificationsManager.triggerImmediateEvent(ShowXML .NOTIFICATION_ID, 0, this, null); } } } }
04-16-2009 11:05 AM
There are 2 approaches you can take to work with email attachments. You can implement a content handler using JSR 172 for the content type of the attachment. This provides the user with a Download Attachment menu option when they open the message. Download Attachment downloads the attachment and then invokes your application to process it.
Alternatively you can use an AttachmentHandler as described here:
How to - Create a custom Attachment Handler on the BlackBerry device
Article Number: DB-00143