title: Accessing gmail via imap using Javamail (behind a proxy)
body: <p>I am using code from on the earlier threads(have pasted the code below as well):</p>
  
  <p><a href="https://stackoverflow.com/questions/61176/getting-mail-from-gmail-into-java-application-using-imap">Getting mail from GMail into Java application using IMAP</a></p>
  
  <p>How do I make this code work if I am behind a proxy?</p>
  
  <p>The connection is getting timed out, I have tried to search for a solution but to no avail. </p>
  
  <pre><code>public static void main(String args[]) {
  
      Properties props = System.getProperties();
      props.setProperty("mail.store.protocol", "imaps");
          try {
              Session session = Session.getDefaultInstance(props, null);
              Store store = session.getStore("imaps");
              store.connect("imap.gmail.com", email, password);
              System.out.println(store);
  
              Folder inbox = store.getFolder("Inbox");
              inbox.open(Folder.READ_ONLY);
              Message messages[] = inbox.getMessages();
              for(Message message:messages) {
              System.out.println(message);
          }
      } catch (NoSuchProviderException e) {
          e.printStackTrace();
          System.exit(1);
      } catch (MessagingException e) {
          e.printStackTrace();
          System.exit(2);
      }
  
  }
  </code></pre>
  
  <p>Thanks,
  Chris.</p>