title: MailConnectException in Java
body: <p>I am try to take unread mails from gmail. There are lots of example code and I used them. The problem is, code doesn't work in every area. For example when I was at home the code works but in working area it is not. I think it is about network settings or different ports etc...</p>
  
  <p>I am using the code that is already implemented in this <a href="https://stackoverflow.com/questions/46663/how-to-send-an-email-by-java-application-using-gmail-yahoo-hotmail">a link</a>. (First answer)</p>
  
  <p>When I run that script at home it works. In my working are, it doesn't and the error message's is at below. </p>
  
  <p>The question is how can i learn the which port should I use ? or Should I do another thing that I miss ? </p>
  
  <p>Any directions, links or codes are appreciated.  </p>
  
  <p><code>com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
    nested exception is:
      java.net.ConnectException: Connection timed out: connect
      at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
      at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
      at javax.mail.Service.connect(Service.java:345)
      at javax.mail.Service.connect(Service.java:226)
      at gmailYeni.SendMail.sendFromGMail(SendMail.java:52)
      at gmailYeni.SendMail.main(SendMail.java:20)</code></p>
  
  <pre><code>    `Caused by: java.net.ConnectException: Connection timed out: connect
  at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
  at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
  at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
  at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
  at java.net.PlainSocketImpl.connect(Unknown Source)
  at java.net.SocksSocketImpl.connect(Unknown Source)
  at java.net.Socket.connect(Unknown Source)
  at java.net.Socket.connect(Unknown Source)
  at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:297)
  at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
  at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
  ... 5 more
  </code></pre>
  
  <p>`</p>