title: Send Email through GMail behind proxy in Java
body: <p>I'm trying to send email through Gmail server in JAVA.<br> Here is my actual <strong>code</strong> :</p>
  
  <pre><code>public static void main(String[] args) throws MessagingException  {
  
      Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
      final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
      // Get a Properties object
      Properties props = System.getProperties();
      props.setProperty("proxySet", "true");
      props.setProperty("http.proxyHost", "xxx.xx.xxx.xx");
      props.setProperty("http.proxyPort", "80");
  
      props.setProperty("mail.smtp.host", "smtp.gmail.com");
      props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
      props.setProperty("mail.smtp.socketFactory.fallback", "false");
      props.setProperty("mail.smtp.port", "465");
      props.setProperty("mail.smtp.socketFactory.port", "465");
      props.put("mail.smtp.auth", "true");
      props.put("mail.debug", "true");
      props.put("mail.store.protocol", "pop3");
      props.put("mail.transport.protocol", "smtp");
  
  
      final String username = "username";
      final String password = "password";
      Session session = Session.getDefaultInstance(props,
              new Authenticator() {
                  protected PasswordAuthentication getPasswordAuthentication() {
                      System.out.println(props.getProperty("http.proxyHost"));
                      return new PasswordAuthentication(username, password);
                  }
              });
  
      // -- Create a new message --
      Message msg = new MimeMessage(session);
  
      // -- Set the FROM and TO fields --
      msg.setFrom(new InternetAddress("username@gmail.com"));
      msg.setRecipients(Message.RecipientType.TO,
              InternetAddress.parse("to@gmail.com", false));
      msg.setSubject("Hello");
      msg.setText("How are you");
      msg.setSentDate(new Date());
      Transport.send(msg);
      System.out.println("Message sent.");
  }
  </code></pre>
  
  <p>This code works perfectly fine on connections without proxy. When I run the same code on my office network which connects through proxy it doesn't work. Please help me to find a solution.</p>
  
  <p>The output which I get when I run the code in office network,</p>
  
  <pre><code>DEBUG: JavaMail version 1.5.2
  DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
  DEBUG: Tables of loaded providers
  DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
  DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
  DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
  DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
  DEBUG SMTP: need username and password for authentication
  null
  DEBUG SMTP: useEhlo true, useAuth true
  DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false
  Exception in thread "main" com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1;
    nested exception is:
      java.net.ConnectException: Connection timed out: connect
      at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2053)
      at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697)
      at javax.mail.Service.connect(Service.java:386)
      at javax.mail.Service.connect(Service.java:245)
      at javax.mail.Service.connect(Service.java:194)
      at javax.mail.Transport.send0(Transport.java:253)
      at javax.mail.Transport.send(Transport.java:124)
      at proxytest.GmailSender.main(GmailSender.java:61)
  Caused by: java.net.ConnectException: Connection timed out: connect
      at java.net.DualStackPlainSocketImpl.connect0(Native Method)
  </code></pre>