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
Developer
tushar_t
Posts: 50
Registered: ‎01-28-2009
My Device: Not Specified
Accepted Solution

Email - how to delete messages from sent folder?

IDE: Blackberry JDE Version 4.5.0.7

Simulator:  About - BlackBerry Smartphone Simulator 2.9.0.52

Model: BlackBerry Curve 8310 smartphone

 

I have a java program which sends an email to a hardcoded email address every 15 mins. The code it uses is from the Knowledge Centre

 

 

//Get the Store from the default mail Session.
Store store = Session.getDefaultInstance().getStore();

//retrieve the sent folder
Folder[] folders = store.list(Folder.SENT);
Folder sentfolder = folders[0];

//create a new message and store it in the sent folder
Message msg = new Message(sentfolder);
Address recipients[] = new Address[1];

try {
recipients[0]= new Address(“user@company.com”, “user”);

//add the recipient list to the message
msg.addRecipients(Message.RecipientType.TO, recipients);

//set a subject for the message
msg.setSubject(“Test email”);

//sets the body of the message
msg.setContent(“This is a test email from my BlackBerry Wireless Handheld”);

//sets priority
msg.setPriority(Message.Priority.HIGH);

//send the message
Transport.send(msg);
}
catch (Exception me) {
System.err.printIn(me);
}

 

The code works perfectly. But I need to modify it such that AFTER an email has been sent, the message is deleted from the Sent Folder. How can this be done?

 

Thanks.

 

 

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Email - how to delete messages from sent folder?

try to use this code:

m.setFlag(Message.Flag.OPENED, true);
Folder folder = m.getFolder();
folder.deleteMessage(m);
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
tushar_t
Posts: 50
Registered: ‎01-28-2009
My Device: Not Specified

Re: Email - how to delete messages from sent folder?

Thanks for that simon. I'm going to try that now, but could you answer these 2 questions anyway:-

1) What does this line of code achieve -  m.setFlag(Message.Flag.OPENED, true);

Setting it to true will mean that the message has been read, but why is that required here?

 

2) The definition of the deleteMessage method is

deleteMessage(Message msg, boolean forceDeleteSaved) 

What's the boolean argument for?

 

Cheers again.

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Email - how to delete messages from sent folder?

1) this is just some small convinience code, you don't have to use it.
a deleted mail is moved to the deleted folder (maybe you can delete it again there, didn't try). if it is unread the user will have an unread message in his deleted folder if he uses a synchronized outlook.

2) deleteMessage is an overloaded method, you do not have to use this version. see the javadoc for an explaination of the parameters.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
tushar_t
Posts: 50
Registered: ‎01-28-2009
My Device: Not Specified

Re: Email - how to delete messages from sent folder?

Thanks. Your solution works perfectly by the way.
New Developer
Teamghost
Posts: 16
Registered: ‎08-04-2011
My Device: Z10 9800

Re: Email - how to delete messages from sent folder?

folder.deleteMessage(m);

this is no longer working with OS 6 ?
I got a lot of not deleted mails, or null pointer exceptions from deleteMessage.

Any ideas?
Das Leben ist wie eine Lawine: Mal rauf mal runter
New Developer
Rdot
Posts: 2
Registered: ‎02-09-2013
My Device: 9860

Re: Email - how to delete messages from sent folder?

[ Edited ]

Hey Guys,

                    I'm having similar problem with deleteMessage(). the message is still present in outbox folder on the device after invoking.

Im using Blackberry 9860 device running  OS 7.1 to test and  Blackberry Java SDK Version 7.1 to build application.

Here is a snippet of the code

 

Store store = Session.getDefaultInstance().getStore();
Folder [] folders = store.list(Folder.SENT);
Folder sentFolder = folders[0];

Message msg = new Message(sentFolder);

try
{
Address toList[] = new Address[1];
// toList[0] = new Address("r_blakey@msn.com", msg.getFolder().getName());
toList[0] = new Address("name@domain.com", "Rdot");

msg.addRecipients(Message.RecipientType.TO,toList);
msg.setContent("Testing 1 2 3");

Transport.send(msg);
sentFolder.deleteMessage(msg);

 

Any hint at why the email isnt being deleted from the outbox would be greatly appreciated.

Thanks in advance