Welcome!

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

Reply
New Developer
Posts: 54
Registered: ‎03-04-2009
My Device: Not Specified

How to retrieve content in email attachments

[ Edited ]

I am using the following code to retrieve attachments and the content within on Blackberry v4.2.1.91. I search for attachments in all messages residing in all folders of all email services configured on my phone. My problem is when I try to get the attachment content I always get a byte[] of length 0 irrespective whether the Attachment is of type SupportedAttachmentPart or UnsupportedAttachmentPart.

Can anybody point out the reasons for the same and suggest an appropriate solution?

 

public class MailTester extends Application implements AttachmentHandler {
public static void main(String[] args) {
tester = new MailTester();
testMail();
tester.enterEventDispatcher();
}

public static void testMail() {
try {

AttachmentHandlerManager mgr = AttachmentHandlerManager.getInstance();
mgr.addAttachmentHandler(tester);
ServiceBook sb = ServiceBook.getSB();
ServiceRecord[] srs = sb.getRecords();

for (int cnt = srs.length; --cnt >= 0;) {

if (srs[cnt].getCid().equals("CMIME")) {
ServiceConfiguration sc = new ServiceConfiguration(srs[cnt]);
Store store = Session.getInstance(sc).getStore();
Folder[] folderArr = store.list();

for(int i=0; i<folderArr.length; i++) {
Folder folder = folderArr[i];
Message[] msgArr = folder.getMessages();

for(int j=0; j<msgArr.length; j++) {
Message msg = msgArr[j];
Object part = msg.getContent();

if(part instanceof Multipart) {
Multipart multipart = (Multipart)part;
System.out.println("multipart :: " + multipart);

for(int k=0; k<multipart.getCount(); k++) {
BodyPart bodypart = multipart.getBodyPart(k);

if(bodypart instanceof SupportedAttachmentPart) {
SupportedAttachmentPart attch = (SupportedAttachmentPart)
bodypart;

byte[] content = (byte[])attch.getContent();
System.out.println("content === " + content);
System.out.println("content length :: " + content.length);
//always prints 0

System.out.println("content :: " + new String(content));
//always prints an empty string
}
else if(bodypart instanceof UnsupportedAttachmentPart) {
UnsupportedAttachmentPart attch = (UnsupportedAttachmentPart)
bodypart;

System.out.println("content :: " + attch.getContent());
//always prints null }
}
}
}
}
}
}
} catch (Exception ex) {
}
}

public boolean supports(String arg0) {
return true;
}

public String menuString() {
return "Open Attch";
}

public void run(Message msg, SupportedAttachmentPart p) {
System.out.println("callback recd in run() :: ");
String attch = new String((byte[])p.getContent());
System.out.println("attch body :: " + attch);
//always prints an empty string
}
}

 

 

 

Message Edited by chintan_kanal on 05-18-2009 04:01 PM
Message Edited by chintan_kanal on 05-18-2009 04:04 PM
Developer
Posts: 5,339
Registered: ‎09-20-2008
My Device: ***
My Carrier: ***

Re: How to retrieve content in email attachments

New Developer
Posts: 54
Registered: ‎03-04-2009
My Device: Not Specified

Re: How to retrieve content in email attachments

[ Edited ]

Had already been thru the link before. Does not help Smiley Sad

In the link "How To - Access HTML email messages" when it comes to the if condition

if (obj instanceof SupportedAttachmentPar) and if (obj instanceof UnsupportedAttachmentPart)

nothing is done except that the booleans _hasSupportedAttachment and _hasUnsupportedAttachment are set as true;.

 

My aim is to retrieve the content within the attachment which I am not able to. After reading several posts on this forum and other resources I realize that content of an UnsupportedAttachmentPart cannot be retrieved before 4.5.x versions. However I am not able to retrieve the content of even a SupportedAttachmentPart as highlighted in my code. Plz help.

 

 

Message Edited by chintan_kanal on 05-18-2009 06:52 PM
New Developer
Posts: 6
Registered: ‎07-20-2009
My Device: Not Specified

Re: How to retrieve content in email attachments

Hi Chintan,

 

I'm getting this same problem. I'm unable to retrieve the contents of my attachments even if it is of SupportedAttachmentPart.

bodypart.getContent() returns 0 bytes even though getSize() returns 10 bytes.

 

Did you manage to solve this problem ?

 

Thanks,

Manhar.

New Developer
Posts: 5
Registered: ‎01-31-2010
My Device: Bold 9700
My Carrier: Vodafone AU

Re: How to retrieve content in email attachments

Did anyone get this resolved?  I am having the same problem.  I have been able to work around the issue for the purpose of my application testing by renaming the attachment x-rimdevice-attachmentname.ext.  But this is not a real solution, as I can't guarantee that emails sent to the device are going to have this prefix on attachment names.

 

I do understand that if the filename prefix isn't present, then the attachment isn't delivered to the device and hence the byte[] returned from getContent is empty.  But surely there is a way of either fetching the attachment, or ensuring that it is delivered to the device in the first place without changing the filename.

 

Thanks,

Garrick

Highlighted
Developer
Posts: 271
Registered: ‎12-24-2010
My Device: BlackBerry 10
My Carrier: Airtel

Re: How to retrieve content in email attachments

For me also same..did you get any solution??  iF yes, then please help me...