title: How can i make the download of emails from GMAIL faster using IMAP and JavaMail?
body: <p>How can I make the download of emails from GMAIL faster, using IMAP and JavaMail?
  Can I do bulk download using IMAP?
  I am saving the emails into MongoDB as well.
  I saw some websites are doing it very quickly. </p>
  
  <h2>Are there any performance tips that I can use here.</h2>
  
  <h2>-----------------------------------------------------</h2>
  
  <hr>
  
  <pre><code>        // Get inbox folder
          Folder inbox = store.getFolder("inbox");
          // SET readonly format (*You can set read and write)
          inbox.open(Folder.READ_ONLY);
  
          // Display email Details
  
          // Inbox email count
          int messageCount = inbox.getMessageCount();
          System.out.println("Total Messages in INBOX:- " + messageCount);
  
          Message[] messages = inbox.getMessages(1, messageCount);
  
          long startTime = System.currentTimeMillis();
  
          // Fetch only necessary headers for each message
          FetchProfile profile = new FetchProfile();
          profile.add(FetchProfile.Item.ENVELOPE);
          profile.add(FetchProfile.Item.FLAGS);
          profile.add(FetchProfile.Item.CONTENT_INFO);
          if (inbox instanceof UIDFolder) {
              profile.add(UIDFolder.FetchProfileItem.UID);
          }
          inbox.fetch(messages, profile);
  
          System.out
                  .println("Time elapsed while fetching message headers;" + (System.currentTimeMillis() - startTime));
  
          ArrayList&lt;Email&gt; al = new ArrayList&lt;Email&gt;();
          for (int i = 1; i &lt; messageCount; i++) {
              //System.out.println("------------------------------------------------------------");
              Email email = new Email();
              email.setId(i + "");
              email.setSenderAddress(extractEmail(messages[i].getFrom()[0].toString()));
              email.setReceivedDate(messages[i].getReceivedDate());
              email.setSentDate(messages[i].getSentDate());
              email.setSubject(messages[i].getSubject());
              email.setMessageContent(messages[i].getContent().toString());
  
              email.setReceiverAddresses(new String[messages[i].getAllRecipients().length]);
              for (int j = 0; j &lt; messages[i].getAllRecipients().length; j++)
                  email.getReceiverAddresses()[j] = extractEmail(
                          messages[i].getAllRecipients()[j].toString());
  
              al.add(email);
              if (i % 2000 == 0) {
                  emailMongoRepository.save(al);
                  System.out.println("----------Bulk Save--------------------------------------------------");
                  al = new ArrayList&lt;Email&gt;();
              }
          }
  
          inbox.close(true);
          store.close();
  
          System.out.println("Time elapsed while fetching message headers;" + (System.currentTimeMillis() - startTime));
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  </code></pre>