javamail - Java Mail API Emails are not displaying in reverse order -


public class testemail {      properties properties = null;     private session session = null;     private store store = null;     private folder inbox = null;     private string username = "xx@gmail.com";  //     private string password = "xx";     public testemail() {      }      public void readmails() throws exception {             properties = new properties();             properties.setproperty("mail.host", "imap.gmail.com");             properties.setproperty("mail.port", "995");             properties.setproperty("mail.transport.protocol", "imaps");             session = session.getinstance(properties,                             new javax.mail.authenticator() {                     protected passwordauthentication getpasswordauthentication() {                             return new passwordauthentication(username, password);                     }             });             try {                      store = session.getstore("imaps");                     store.connect();                     inbox = store.getfolder("inbox");                     inbox.open(folder.read_only);                      //message messages[] = inbox.search(new flagterm(new flags(flag.seen), false));                     message messages[]=inbox.getmessages();             //      system.out.println("number of mails = " + messages.length);                      arrayutils.reverse(messages);                     ( message message : messages ) {                             system.out.println("subject: "+ message.getsubject());                             //if(message.getsubject().tostring()=="suppliers match search: suto in future mode")                             //{                             address[] = message.getfrom();                             system.out.println("-------------------------------");                             system.out.println("date : " + message.getsentdate());                             //system.out.println("from : " + from[0]);                             //system.out.println("subject: " + message.getsubject());                             //system.out.println("content :");                              object content = message.getcontent();                             multipart multipart = (multipart) content;                             //procesmultipart(multipart);                              system.out.println("--------------------------------");                             //}                             //else                             //{                                     //system.out.println("not found");                             //}                     }                     inbox.close(true);                     store.close();             }             catch (nosuchproviderexception e)             {                     e.printstacktrace();             } catch (messagingexception e) {                     e.printstacktrace();             }     }       public void procesmultipart(multipart content) throws exception {              int multipartcount = content.getcount();             (int = 0; < multipartcount; i++) {                     bodypart bodypart = content.getbodypart(i);                     object o;                     o = bodypart.getcontent();                     if (o instanceof string) {                             system.out.println(o);                     } else if (o instanceof multipart) {                             procesmultipart((multipart) o);                     }             }     }      public static void main(string[] args) throws exception {             testemail sample = new testemail();             sample.readmails();     }} 

output :

subject: stay more organised gmail's inbox

date : fri aug 14 19:46:39 ist 2015

subject: sign-in attempt prevented

date : fri aug 14 21:53:49 ist 2015

subject: suppliers match search: jan 28 vani

date : sat aug 15 11:55:33 ist 2015

subject: suppliers match search: search1

date : sat aug 15 11:55:35 ist 2015

subject: suppliers match search: 123

date : sat aug 15 11:55:29 ist 2015

subject: suppliers match search:

date : sat aug 15 11:55:32 ist 2015

subject: suppliers match search: ttt

date : sat aug 15 11:55:30 ist 2015

subject: suppliers match search: suto in future mode

date : sat aug 15 11:55:33 ist 2015

subject: suppliers match search: test 13 mar 2012 automotive

date : sat aug 15 11:55:30 ist 2015

it seems folder#getmessages() not guarantee specific order of messages. have sort them yourself:

arrayutils.sort(messages, new comparator<message>() {     @override     public int compareto(message m1, message m2) {         return m2.getreceiveddate().compareto(m1.getreceiveddate());     } }); 

Comments

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -