我正在尝试使用android中的javamail api获取no.of消息,no.of已删除消息和no.of总消息的详细信息。但是对于已删除的消息,我总是得到-1。我找不到是什么原因/错误所以请帮助我。这是我的代码 class Readmails扩展了AsyncTask {
Folder inbox;
Folder inbox2;
Properties props = System.getProperties();
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
props.setProperty("mail.store.protocol", "imaps");
try
{
/* Create the session and get the store for read the mail. */
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com","test123@gmail.com", "testmycode12345");
/* Mention the folder name which you want to read. */
inbox = store.getFolder("Inbox");
System.out.println("No of Unread Messages : " + inbox.getUnreadMessageCount());
System.out.println("No of New Messages : " + inbox.getNewMessageCount());
System.out.println("No of Deleted Messages : " +inbox.getDeletedMessageCount());
System.out.println("No of total Messages : " + inbox.getMessageCount());
System.out.println("No of Type Messages : " + inbox.getType());
/*Open the inbox using store.*/
inbox.open(Folder.READ_ONLY);
/* Get the messages which is unread in the Inbox*/
Message messages[] = inbox.search(new FlagTerm(new Flags(Flag.SEEN), false));
/* Use a suitable FetchProfile */
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.CONTENT_INFO);
inbox.fetch(messages, fp);
try
{
inbox.close(true);
store.close();
}
catch (Exception ex)
{
System.out.println("Exception arise at the time of read mail");
ex.printStackTrace();
}
}
catch (NoSuchProviderException e)
{
e.printStackTrace();
System.exit(1);
}
catch (MessagingException e)
{
e.printStackTrace();
System.exit(2);
}
return null;
}
}
答案 0 :(得分:1)
在打开收件箱之前,您正在呼叫getDeletedMessageCount()
。
请参阅代码中的this comment,它表示在已关闭的文件夹中返回-1。将您的来电移至inbox.open()
println