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
Contributor
Posts: 47
Registered: ‎02-20-2009
My Device: Torch
My Carrier: AT&T
Accepted Solution

Modifying an email and keeping attachments

I'm trying to modify an email body when it's sent (after you click send, before it goes out). I have everything working, but when I modufy the body I lose attachments.

 

email is in a Message type.

 

//works

message.setSubject("abc")

 

//doesn't work

message.setContent("newtext" + message.getBodyText());

 

I just want to prepend text to the textbody...is there an easy way to do that?

 

Thanks.

 

---
LVM Engineering, Inc.
www.LVMEngineering.com

Don't forget to "Kudo" the helpful posts!
Contributor
Posts: 47
Registered: ‎02-20-2009
My Device: Torch
My Carrier: AT&T

Re: Modifying an email and keeping attachments

I have a partial solution...

 

Object pt = (Object) message.getContent();
if (pt instanceof BodyPart) {
String text = message.getBodyText();
message.setContent(banner + text);
} else if (pt instanceof Multipart) {
TextBodyPart tbp = new TextBodyPart(((Multipart) pt), banner);
((Multipart) pt).addBodyPart(tbp, 0);

...

 

The problem is that while this DOES work, the attachment loses its filename... a jpg comes through as some random name with a .tmp extension. If I change the ext to .jpg, the file is there.

 

Like I say...all I'm trying to do is prepend some text to an email body...that's all... I would hope there's some obvious and easy way that I'm missing...

 

---
LVM Engineering, Inc.
www.LVMEngineering.com

Don't forget to "Kudo" the helpful posts!
Contributor
Posts: 47
Registered: ‎02-20-2009
My Device: Torch
My Carrier: AT&T

Re: Modifying an email and keeping attachments

To make this simple (and to bump the thread) all I want to do is prepend "Hello World" to the first BodyPart of a message without altering the rest of the message.

 

How would I go about doing that?

 

My current methods show inconsistent behavior when attachments are involved...

 

Much appreciated!

 

--lee

---
LVM Engineering, Inc.
www.LVMEngineering.com

Don't forget to "Kudo" the helpful posts!
Contributor
Posts: 47
Registered: ‎02-20-2009
My Device: Torch
My Carrier: AT&T

Changes in 6.0!

So now I'm finding that my solution has worked all along, just not in OS6. What has changed WRT the MultiPart class in 6.0 since 5.0?

 

Anyone know?

---
LVM Engineering, Inc.
www.LVMEngineering.com

Don't forget to "Kudo" the helpful posts!
Contributor
Posts: 47
Registered: ‎02-20-2009
My Device: Torch
My Carrier: AT&T

Changes between 4.6, 5.0, 6.0?

Correction...works on a 4.6 device, not a 5.x or 6.x... Below is the latest code in question:

 

 

Object pt = (Object) msg.getContent();

if (pt instanceof BodyPart) {
   //This works Fine
   String text = msg.getBodyText();
   msg.setContent(banner + text);
} else if (pt instanceof Multipart) {
   //This Doesn't
   TextBodyPart tbp = new TextBodyPart(((Multipart) pt),banner);
   ((Multipart) pt).addBodyPart(tbp, 0);
   msg.setContent(pt);
}
Transport.send(msg);

 

 

The problem is that when I send the attachment (called test.jpg) it gets sent as "SU1289433050093.tmp", or similar... If I change the name, the file works! On OS4.6, the code works as-is and sends the correct filename (unplanned).

 

Hoping someone will chime in soon... Could really use some help...

 

---
LVM Engineering, Inc.
www.LVMEngineering.com

Don't forget to "Kudo" the helpful posts!
Contributor
Posts: 47
Registered: ‎02-20-2009
My Device: Torch
My Carrier: AT&T

Re: Changes between 4.6, 5.0, 6.0?

[ Edited ]

Two problems solved here, hopefully this is of help to someone else:

 

1) My solution for adding text to the body of an email without affecting attachments:

 

Object pt = (Object) msg.getContent();
if (pt instanceof BodyPart) {
String text = msg.getBodyText();
msg.setContent(banner + text);
} else if (pt instanceof Multipart) {
Multipart mp = new Multipart();
TextBodyPart tbp = new TextBodyPart(mp,banner);
mp.addBodyPart(tbp);
msg.setContent(null);
msg.setContent(mp);
msg.setContent(pt);
}

 

 

2) Message.setContent(Object) does NOT clear the existing objects, it pushes them. This explains my "setContent(null)".

 

3) If you just do a setContent(...) on a Multipart, you'll lose your attachments...VERY STRANGE given #2, huh? I repeated this hundreds of times...possibly a bug...

 

4) The temporary file name I was getting (e.g. SU1289433050093.tmp) is the name of the resized image file. You know how BB asks if you want to sent the original or a smaller version? That!. I'm not sure how to fix that one, so it'll be a low-pri bug for now...

 

(AND SOMEONE KUDO ME!!)

 

---
LVM Engineering, Inc.
www.LVMEngineering.com

Don't forget to "Kudo" the helpful posts!
Developer
Posts: 250
Registered: ‎07-18-2008
My Device: Not Specified

Re: Changes between 4.6, 5.0, 6.0?

[ Edited ]

Hi,

 

I'd like to do the same thing but on an incoming message (using a FolderListener) but it doesn't work, some of the attachments are lost when I modify the email body.

 

Does anyone know why some attachments are lost when an incoming message is modified? Is this because the attachments are processed by the BES?

 

Regards,

 

Pierre

Highlighted
Developer
Posts: 250
Registered: ‎07-18-2008
My Device: Not Specified

Re: Changes between 4.6, 5.0, 6.0?

Anyone?

Contributor
Posts: 47
Registered: ‎02-20-2009
My Device: Torch
My Carrier: AT&T

Re: Changes between 4.6, 5.0, 6.0?

Make sure you're adding the parts back to the multipart. My Guess (only a guess) is that you're modifying the text, breaking the object, then returning that object. This is similar to the voodoo I had to do with my example above... Post code for more help...

---
LVM Engineering, Inc.
www.LVMEngineering.com

Don't forget to "Kudo" the helpful posts!
Developer
Posts: 250
Registered: ‎07-18-2008
My Device: Not Specified

Re: Changes between 4.6, 5.0, 6.0?

Well, I have replaced my code with yours to check and it still doesn't work as expected.

I'm using the 4.2 sdk because my application must work on older devices.