title: JavaMail - Using proxy server since reading gmail code isn't working when connected to office network
body: <p>Below code to read gmail works fine when i'm not connected to office network</p>
  
  <pre><code>      Properties props = new Properties();        
        props.setProperty("mail.imap.ssl.enable", "true");     
        Session mailSession = Session.getInstance(props); 
        mailSession.setDebug(true);
        Store mailStore = mailSession.getStore("imap");
        mailStore.connect("imap.gmail.com", "abc@gmail.com", "pwd");
  </code></pre>
  
  <p>However same code doesn't work when connected to office network. Error is :</p>
  
  <pre><code>DEBUG: setDebug: JavaMail version 1.6.2
  DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle]
  DEBUG IMAP: mail.imap.fetchsize: 16384
  DEBUG IMAP: mail.imap.ignorebodystructuresize: false
  DEBUG IMAP: mail.imap.statuscachetimeout: 1000
  DEBUG IMAP: mail.imap.appendbuffersize: -1
  DEBUG IMAP: mail.imap.minidletime: 10
  DEBUG IMAP: closeFoldersOnStoreFailure
  DEBUG IMAP: trying to connect to host "imap.gmail.com", port 993, isSSL true
  com.sun.mail.util.MailConnectException: Couldn't connect to host, port: imap.gmail.com, 993; timeout -1;
    nested exception is:
      java.net.UnknownHostException: imap.gmail.com
      at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:740)
      at javax.mail.Service.connect(Service.java:366)
      at javax.mail.Service.connect(Service.java:246)
  Caused by: java.net.UnknownHostException: imap.gmail.com
      at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:220)
      at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
      at java.base/java.net.Socket.connect(Socket.java:608)
      at java.base/java.net.Socket.connect(Socket.java:557)
      at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:359)
      at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
      at com.sun.mail.iap.Protocol.&lt;init&gt;(Protocol.java:134)
      at com.sun.mail.imap.protocol.IMAPProtocol.&lt;init&gt;(IMAPProtocol.java:131)
      at com.sun.mail.imap.IMAPStore.newIMAPProtocol(IMAPStore.java:763)
      at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:698)
      ... 4 more
  </code></pre>
  
  <p>After googling found suggestion to use proxy . As per <a href="https://javaee.github.io/javamail/FAQ#proxy" rel="nofollow noreferrer">https://javaee.github.io/javamail/FAQ#proxy</a> Starting with JavaMail 1.6.0 we can set proxy details properties. So added these properties to the code :</p>
  
  <pre><code>        Properties props = new Properties();
          props.setProperty("mail.imap.ssl.enable", "true");
          props.setProperty("mail.protocol.proxy.host", "hostdetails");
          props.setProperty("mail.protocol.proxy.port", "80");
          //props.setProperty("mail.imap.socks.host", "hostdetails");
          //props.setProperty("mail.imap.socks.port", "80");
          Session mailSession = Session.getInstance(props);
          mailSession.setDebug(true);
          Store mailStore = mailSession.getStore("imap");
          mailStore.connect("imap.gmail.com", "abc@gmail.com", "pwd");
  </code></pre>
  
  <p>Got the same error. Tried adding <code>props.setProperty("mail.imap.socks.host", "hostdetails");</code> as well but got the same error. Any idea how to make it work?</p>