public static void main(String[] args) throws MessagingException, IOException {
    IMAPFolder folder = null;
    Store store = null;
    String subject = null;
    Flag flag = null;
    try {
      Properties props = System.getProperties();
      props.setProperty("mail.store.protocol", "imaps");
      props.setProperty("mail.imap.host", "imap.googlemail.com");
      SimpleAuthenticator authenticator =
          new SimpleAuthenticator("*****@*****.**", "hhy8611hhyy");
      Session session = Session.getDefaultInstance(props, null);
      // Session session = Session.getDefaultInstance(props, authenticator);

      store = session.getStore("imaps");
      //          store.connect("imap.googlemail.com","*****@*****.**", "hhy8611hhyy");
      // store.connect("imap.googlemail.com","*****@*****.**", "hhy8611hhyy");

      store.connect("*****@*****.**", "hhy8611hhy");
      // folder = (IMAPFolder) store.getFolder("[Gmail]/Spam"); // This doesn't work for other email
      // account
      folder = (IMAPFolder) store.getFolder("inbox"); // This works for both email account

      if (!folder.isOpen()) folder.open(Folder.READ_WRITE);
      Message[] messages = messages = folder.getMessages(150, 150); // folder.getMessages();
      System.out.println("No of get Messages : " + messages.length);
      System.out.println("No of Messages : " + folder.getMessageCount());
      System.out.println("No of Unread Messages : " + folder.getUnreadMessageCount());
      System.out.println("No of New Messages : " + folder.getNewMessageCount());
      System.out.println(messages.length);
      for (int i = 0; i < messages.length; i++) {

        System.out.println(
            "*****************************************************************************");
        System.out.println("MESSAGE " + (i + 1) + ":");
        Message msg = messages[i];
        // System.out.println(msg.getMessageNumber());
        // Object String;
        // System.out.println(folder.getUID(msg)

        subject = msg.getSubject();

        System.out.println("Subject: " + subject);
        System.out.println("From: " + msg.getFrom()[0]);
        System.out.println("To: " + msg.getAllRecipients()[0]);
        System.out.println("Date: " + msg.getReceivedDate());
        System.out.println("Size: " + msg.getSize());
        System.out.println(msg.getFlags());
        System.out.println("Body: \n" + msg.getContent());
        System.out.println(msg.getContentType());
      }
    } finally {
      if (folder != null && folder.isOpen()) {
        folder.close(true);
      }
      if (store != null) {
        store.close();
      }
    }
  }
 public static byte[] messageBytes(Message m) {
   byte[] b = new byte[m.getSize()];
   m.getBytes(b, 0);
   return b;
 }
 private void fetchEnvelope(
     List<Pop3Message> messages, MessageRetrievalListener<Pop3Message> listener)
     throws IOException, MessagingException {
   int unsizedMessages = 0;
   for (Message message : messages) {
     if (message.getSize() == -1) {
       unsizedMessages++;
     }
   }
   if (unsizedMessages == 0) {
     return;
   }
   if (unsizedMessages < 50 && mMessageCount > 5000) {
     /*
      * In extreme cases we'll do a command per message instead of a bulk request
      * to hopefully save some time and bandwidth.
      */
     for (int i = 0, count = messages.size(); i < count; i++) {
       Pop3Message message = messages.get(i);
       if (listener != null) {
         listener.messageStarted(message.getUid(), i, count);
       }
       String response =
           executeSimpleCommand(
               String.format(
                   Locale.US, LIST_COMMAND + " %d", mUidToMsgNumMap.get(message.getUid())));
       String[] listParts = response.split(" ");
       // int msgNum = Integer.parseInt(listParts[1]);
       int msgSize = Integer.parseInt(listParts[2]);
       message.setSize(msgSize);
       if (listener != null) {
         listener.messageFinished(message, i, count);
       }
     }
   } else {
     Set<String> msgUidIndex = new HashSet<String>();
     for (Message message : messages) {
       msgUidIndex.add(message.getUid());
     }
     int i = 0, count = messages.size();
     String response = executeSimpleCommand(LIST_COMMAND);
     while ((response = readLine()) != null) {
       if (response.equals(".")) {
         break;
       }
       String[] listParts = response.split(" ");
       int msgNum = Integer.parseInt(listParts[0]);
       int msgSize = Integer.parseInt(listParts[1]);
       Pop3Message pop3Message = mMsgNumToMsgMap.get(msgNum);
       if (pop3Message != null && msgUidIndex.contains(pop3Message.getUid())) {
         if (listener != null) {
           listener.messageStarted(pop3Message.getUid(), i, count);
         }
         pop3Message.setSize(msgSize);
         if (listener != null) {
           listener.messageFinished(pop3Message, i, count);
         }
         i++;
       }
     }
   }
 }