title: Getting mail from GMail into Java application using IMAP
body: <p>I want to access messages in Gmail from a Java application using <a href="http://www.ing.iac.es/~docs/external/java/javamail/javadocs/index.html" rel="nofollow noreferrer">JavaMail</a> and <a href="https://en.wikipedia.org/wiki/Internet_Message_Access_Protocol" rel="nofollow noreferrer">IMAP</a>. Why am I getting a <em><a href="https://docs.oracle.com/javase/7/docs/api/java/net/SocketTimeoutException.html" rel="nofollow noreferrer">SocketTimeoutException</a></em> ?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>Properties props = System.getProperties();
  props.setProperty("mail.imap.host", "imap.gmail.com");
  props.setProperty("mail.imap.port", "993");
  props.setProperty("mail.imap.connectiontimeout", "5000");
  props.setProperty("mail.imap.timeout", "5000");
  
  try {
      Session session = Session.getDefaultInstance(props, new MyAuthenticator());
      URLName urlName = new URLName("imap://MYUSERNAME@gmail.com:MYPASSWORD@imap.gmail.com");
      Store store = session.getStore(urlName);
      if (!store.isConnected()) {
          store.connect();
      }
  } catch (NoSuchProviderException e) {
      e.printStackTrace();
      System.exit(1);
  } catch (MessagingException e) {
      e.printStackTrace();
      System.exit(2);
  }
  </code></pre>
  
  <p>I have set the timeout values so that it wouldn't take "forever" to timeout. Also, <em>MyAuthenticator</em> also has the username and password, which seems redundant with the URL. Is there another way to specify the protocol? (I didn't see it in the JavaDoc for <a href="https://en.wikipedia.org/wiki/Internet_Message_Access_Protocol" rel="nofollow noreferrer">IMAP</a>.)</p>