title: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25;
body: <p>The Code I'm Using to send a simple mail</p>
  
  <pre><code>import javax.mail.*;
  import javax.mail.internet.*;
  import java.util.*;
  import java.io.*;
  import javax.activation.DataHandler;
  import javax.activation.FileDataSource;
  
  public class SendMailUsingAuthentication
  {
  
    private static final String SMTP_HOST_NAME = "smtp.gmail.com";
    private static final String SMTP_AUTH_USER = "myemail@gmail.com";
    private static final String SMTP_AUTH_PWD  = "mypassword";
  
    public static void main(String args[]) throws Exception
    {
  
    }
  
    public void postMail( String recipients[ ], String subject,String message , String from) throws MessagingException
    {
      try {
          boolean debug = false;
  
          Properties props = new Properties();
          props.put("mail.smtp.host", SMTP_HOST_NAME);
          props.put("mail.smtp.starttls.enable", "true");
          props.put("mail.smtp.auth", "true");
          Authenticator auth = new SMTPAuthenticator();
          Session session = Session.getDefaultInstance(props, auth);
          session.setDebug(debug);
  
          Message msg = new MimeMessage(session);
  
          InternetAddress addressFrom = new InternetAddress(from);
          msg.setFrom(addressFrom);
          InternetAddress[] addressTo = new InternetAddress[recipients.length];
          for (int i = 0; i &lt; recipients.length; i++) {
              addressTo[i] = new InternetAddress(recipients[i]);
          }
          msg.setRecipients(Message.RecipientType.TO, addressTo);
  
          msg.setSubject(subject);
  
          msg.setContent(message, "text/plain");
  
          Transport.send(msg);
  
      } 
      catch (Throwable e) 
      {
          e.printStackTrace();
      }
   }
  /**
  * SimpleAuthenticator is used to do simple authentication
  * when the SMTP server requires it.
  */
  private class SMTPAuthenticator extends javax.mail.Authenticator
  {
      public PasswordAuthentication getPasswordAuthentication()
      {
          String username = SMTP_AUTH_USER;
          String password = SMTP_AUTH_PWD;
          return new PasswordAuthentication(username, password);
      }
  }
  }
  </code></pre>
  
  <p>When I'm running this code on my Local machine, it's working fine.... But When I deployed it on the server it's giving me this exception</p>
  
  <pre><code>javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25;
    nested exception is:
      java.net.ConnectException: Connection timed out
      at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
      at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
      at javax.mail.Service.connect(Service.java:297)
      at javax.mail.Service.connect(Service.java:156)
      at javax.mail.Service.connect(Service.java:105)
      at javax.mail.Transport.send0(Transport.java:168)
      at javax.mail.Transport.send(Transport.java:98)
      at SendMailUsingAuthentication.postMail(SendMailUsingAuthentication.java:97)
      at RegistrationServlet.doGet(RegistrationServlet.java:98)
      at RegistrationServlet.doPost(RegistrationServlet.java:125)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
      at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:196)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
      at java.lang.Thread.run(Thread.java:662)
  Caused by: java.net.ConnectException: Connection timed out
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
      at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
      at java.net.Socket.connect(Socket.java:529)
      at java.net.Socket.connect(Socket.java:478)
      at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
      at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
      at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
  </code></pre>