java - Cannot fetch Gmail inbox with JavaMail POP -
i'm trying fetch unread messages gmail inbox javamail, can't. retrieve archived messages (from 2011!!!) , don't know why or how it.
here code:
public list<defaultmessage> getlatestnthmessages(integer numberofmessages) throws exception { urlname url = new urlname("pop3", "pop.gmail.com", 995, "",username, password); store store = new pop3sslstore(pullsession, url); store.connect(); folder inbox = store.getfolder("inbox"); inbox.open(folder.read_write); searchterm st = new flagterm(new flags(flags.flag.seen), false); list<message> msgs = arrays.aslist(inbox.search(st)).stream() .sorted((m1, m2) -> m2.getmessagenumber() - m1.getmessagenumber()) .limit(numberofmessages) .collect(collectors.tolist()); list<defaultmessage> listofmessages = new arraylist<>(); (message message : msgs) { listofmessages.add(wrappertomessage(message)); } return listofmessages; }
pullsession
instantiated follows:
properties pullprops = new properties(); pullprops.put("mail.pop3.host", pullhost); pullprops.put("mail.pop3.username", username); pullprops.put("mail.pop3.port", pullport); pullprops.put("mail.pop3.socketfactory.port", pullport); pullprops.put("mail.pop3.socketfactory.fallback", "false"); pullprops.put("mail.pop3.socketfactory.class", "javax.net.ssl.sslsocketfactory"); pullprops.put("mail.pop3.auth", "true"); pullsession = session.getinstance(pullprops, null); pullsession.setdebug(true);
check gmail settings pop3.
also, there's lots of things can improve in code, although they're not source of problem. start fixing common javamail mistakes.
you should not creating pop3sslstore directly. use gmail example code in javamail faq.
Comments
Post a Comment