Reputation: 11205
I have implemented a service which retreives the inbox and implements the MessageCountListener interface for listening to new email arrivals.But on new email arrival, it does not gets notified! What may be the reason and what else can be done? Here is the code:
public class EmailRetreiverService extends Service implements MessageCountListener{
public static final Vector v=new Vector();
public static final Vector nwmsg=new Vector();
Message[] m=null;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate(){
Log.d("EmailRetreiverStarted"," ");
ConvertToSmtp cts=new ConvertToSmtp("[email protected]","mypassword"," "," "," ", " ");
Folder folder=cts.retreiveInbox();
try {
m=folder.getMessages();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i=0;i<m.length;i++){
v.add(m[i]);
}
Log.d("EmailRetreiverMessageCount",new Integer(m.length).toString());
Collections.reverse(v);
/*folder.addMessageCountListener(new MessageCountAdapter(){
public void messagesAdded(MessageCountEvent ev) {
Log.d("MessageListener","message listner invoked.");
Message[] msgs = ev.getMessages();
TTSservice.say("Attention! "+msgs.length+" new messages have arrived now.Kindly retreive inbox again!");
Collections.reverse(v);
for (int i = 0; i < msgs.length; i++) {
v.add(msgs[i]);
//System.out.println("Got " + msgs.length + " new messages");
}
Collections.reverse(v);
// Just dump out the new messages
}
});*/
folder.addMessageCountListener(this);
}
@Override
public void onDestroy(){
v.removeAllElements();
}
@Override
public void messagesAdded(MessageCountEvent arg0) {
// TODO Auto-generated method stub
Log.d("EmailService","MessageArrived!");
}
@Override
public void messagesRemoved(MessageCountEvent arg0) {
// TODO Auto-generated method stub
Log.d("EmailService","MessageRemoved!");
}
} It must be noted that the service is successfully retreiving the inbox.But just does not get notified.
Upvotes: 3
Views: 859
Reputation: 29961
You need to do something to cause JavaMail to receive the notification from the server of new messages. A simple approach is to call the getMessageCount() method periodically. Another approach is to use the IMAP-specific idle() method, which requires dedicating a thread to calling that method.
Upvotes: 5