title: Exception in sending email from java via gmail
body: <p>I am getting the following exception while trying send an email from java via gmail.</p>
  
  <pre><code>javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
  nested exception is:
  java.net.ConnectException: Connection refused: connect
  at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
  at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
  at javax.mail.Service.connect(Service.java:288)
  at javax.mail.Service.connect(Service.java:169)
  at Mail.Send(Mail.java:75)
  at Mail.main(Mail.java:85)
  Caused by: java.net.ConnectException: Connection refused: connect
  at java.net.DualStackPlainSocketImpl.connect0(Native Method)
  at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
  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 sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
  at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
  at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
  at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
  at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
  ... 5 more
  </code></pre>
  
  <p>I referred <a href="http://www.tutorialspoint.com/javamail_api/javamail_api_gmail_smtp_server.htm" rel="nofollow noreferrer">http://www.tutorialspoint.com/javamail_api/javamail_api_gmail_smtp_server.htm</a> and <a href="https://stackoverflow.com/questions/3649014/send-email-using-java/3649148#3649148">Send email using java</a> but same exception is coming both for SSL and TLS. Any fix for this?</p>
  
  <p>Thanks in advance.</p>