title: How to know Incoming Email Message ID to get message contents
body: <p>I want to get Incoming Email Message ID for get message contents. I've applied this code to listen change in message counts. </p>
  
  <pre><code>folder.addMessageCountListener(new MessageCountListener() {
  
              public void messagesAdded(MessageCountEvent e) {
                  System.out.println("Message Count Event Fired");
              }
  
              public void messagesRemoved(MessageCountEvent e) {
                  System.out.println("Message Removed Event fired");
              }
          });
  
          folder.addMessageChangedListener(new MessageChangedListener() {
  
              public void messageChanged(MessageChangedEvent e) {
                  System.out.println("Message Changed Event fired");
              }
          });
  </code></pre>
  
  <p>the above code works fine for IMAP server, whenever a message added or removed. But i want to know which Message has been removed or Added. </p>
  
  <p>Kindly help. the full code is, </p>
  
  <p>MainActivity:</p>
  
  <pre><code>String host = "imap.gmail.com";
  String mailStoreType = "imap";
  String username = "EmailAddress@gmail.com";
  String password = "****";
  
  CheckingMails.check(host, mailStoreType, username, password);
  </code></pre>
  
  <p>CheckingMails:</p>
  
  <pre><code>public class CheckingMails {
  
  public static void check(String host, String storeType, String user,
                           String password) {
     boolean supportsIdle = false;
          IMAPFolder folder = null;
          int freq = 2000;
          try {
  
              Properties props = System.getProperties();
              props.setProperty("mail.store.protocol", "imaps");
              Session session = Session.getDefaultInstance(props, null);
              final javax.mail.Store store = session.getStore("imaps");
  
              store.connect(host, user, password);
  
              folder = (IMAPFolder) store.getFolder("Inbox");
              folder.open(IMAPFolder.READ_WRITE);
              Log.d("fhjg43524318778", folder.toString());
  
              try {
                  folder = (IMAPFolder) store.getFolder("Inbox");
                  folder.open(IMAPFolder.READ_WRITE);
                  SearchTerm sender = new FromTerm(new InternetAddress("zohaibsvu@gmail.com"));
  
                  // Getting from specific email
                  Message[] message = folder.search(sender);
  
                  Log.d("fghjgh564", String.valueOf(message.length));
  
                  for (int i = 1; i &lt; message.length; i++) {
                      Message writePart = message[i];
                      int id = message[i].getMessageNumber();
                      String from = message[i].getFrom()[0].toString();
                      String subject = message[i].getSubject();
                      Log.d("dfgh3423", "# "+id+" From "+from+", subject "+subject);
                  }
              } catch (AddressException  y) {
  
              } catch (MessagingException u) {
  
              }
  
          folder.addMessageCountListener(new MessageCountListener() {
  
              public void messagesAdded(MessageCountEvent e) {
                      Log.d("asfd3565678", "Message Count Event Fired");
              }
  
              public void messagesRemoved(MessageCountEvent e) {
                      Log.d("asfd3565678", "Message Count Event Fired");
              }
          });
  
          folder.addMessageChangedListener(new MessageChangedListener() {
  
              public void messageChanged(MessageChangedEvent e) {
                      Log.d("asfd3565678", "Message Count Event Fired");
              }
          });
  
  
          // Check mail once in "freq" MILLIseconds
          int freq = 2000;
          boolean supportsIdle = false;
          try {
              if (emailFolder instanceof IMAPFolder) {
                  IMAPFolder f = (IMAPFolder) emailFolder;
                  f.idle();
                  supportsIdle = true;
              }
          } catch (FolderClosedException fex) {
              throw fex;
          } catch (MessagingException mex) {
              supportsIdle = false;
          }
          for (; ; ) {
              if (supportsIdle &amp;&amp; emailFolder instanceof IMAPFolder) {
                  IMAPFolder f = (IMAPFolder) emailFolder;
                  f.idle();
                  System.out.println("IDLE done");
              } else {
                  Thread.sleep(freq); // sleep for freq milliseconds
  
                  // This is to force the IMAP server to send us
                  // EXISTS notifications.
                  emailFolder.getMessageCount();
              }
          }
  
  
  
          /*
          // retrieve the messages from the folder in an array and print it
          Message[] messages = emailFolder.getMessages();
          System.out.println("messages.length---" + messages.length);
  
          for (int i = 0, n = messages.length; i &lt; n; i++) {
              Message message = messages[i];
              System.out.println("---------------------------------");
              System.out.println("Email Number " + (i + 1));
              System.out.println("Subject: " + message.getSubject());
              System.out.println("From: " + message.getFrom()[0]);
              System.out.println("Text: " + message.getContent().toString());
  
          }
  
          */
  
          //close the store and folder objects
       //   emailFolder.close(false);
       //   store.close();
  
      } catch (NoSuchProviderException e) {
          e.printStackTrace();
      } catch (MessagingException e) {
          e.printStackTrace();
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  
  
  
  
  
  }
  </code></pre>