我已经实现了一项服务,该服务可以检索收件箱并实现MessageCountListener接口,以便收听新的电子邮件到达。但是在新电子邮件到达时,它不会收到通知!可能是什么原因以及还有什么可以做? 这是代码:
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("myemail@gmail.com","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!");
}
} 必须注意的是,该服务已成功检索收件箱。但只是没有收到通知。
答案 0 :(得分:5)
您需要做一些事情来使JavaMail从服务器接收新消息的通知。一种简单的方法是定期调用getMessageCount()方法。另一种方法是使用特定于IMAP的idle()方法,该方法需要专用线程来调用该方法。