这是计算gmail收件箱中邮件数量的代码.
Properties props = new Properties();
props.put("mail.pop3.host" , "pop.gmail.com");
props.put("mail.pop3.user" , "username");
props.put("mail.pop3.socketFactory" , 995 );
props.put("mail.pop3.socketFactory.class" , "javax.net.ssl.SSLSocketFactory" );
props.put("mail.pop3.port" , 995);
Session session = Session.getDefaultInstance(props , new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication( "username" , "password");
}
});
try {
Store store = session.getStore("pop3");
store.connect("pop.gmail.com" , "username" , "password");
Folder fldr = store.getFolder("INBOX");
fldr.open(Folder.HOLDS_MESSAGES);
int count = fldr.getMessageCount();
System.out.println(count);
} catch(Exception exc) {
System.out.println(exc + " error");
}
我得到的计数等于7,但我应该得到3,因为我在收件箱中只有3条消息.
解决方法:
在GMAIL POP3设置中,您应该仅为当前时刻收到的电子邮件启用POP访问,这是标准的GMAIL行为.
When you enable POP, all messages are downloaded to your client, except for Spam, Trash, and Chats. If you don’t want messages that you send from the web interface downloaded to your mail client’s inbox, we suggest creating a filter within your client. You may want to contact your mail client’s customer service department for instructions on how to categorize downloaded messages.
See the GMAIL troubleshooting article
GMAIL中的AFAIK选择性同步仅适用于IMAP协议.