title: Sending mail using gmail from behind a proxy using java
body: <p>My current code is (I am using correct username and password in actual code) :-</p>
  
  <pre><code>            public class MailTry {
  
                  public static void main(String[] args) {
  
                      final String username = "user@gmail.com";
                      final String password = "pass";
  
                      Properties props = new Properties();
                      props.put("mail.smtp.auth", "true");
                      props.put("mail.smtp.starttls.enable", "true");
                      props.put("mail.smtp.host", "smtp.gmail.com");
                      props.put("mail.smtp.port", "587");
  
                      Session session = Session.getInstance(props,
                        new javax.mail.Authenticator() {
                          protected PasswordAuthentication getPasswordAuthentication() {
                              return new PasswordAuthentication(username, password);
                          }
                        });
  
                      try {
  
                          Message message = new MimeMessage(session);
                          message.setFrom(new InternetAddress(username));
                          message.setRecipients(Message.RecipientType.TO,
                              InternetAddress.parse("to-email@gmail.com"));
                          message.setSubject("Testing Subject");
                          message.setText("Dear Mail Crawler,"
                              + "\n\n No spam to my email, please!");
  
                          Transport.send(message);
  
                          System.out.println("Done");
  
                      } catch (MessagingException e) {
                          throw new RuntimeException(e);
                      }
                  }
              }
  </code></pre>
  
  <p>I am getting following exceptions in above code:-</p>
  
  <pre><code>            Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
                nested exception is:
                  java.net.ConnectException: Connection timed out: connect
                  at MailTry.main(MailTry.java:63)
              Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
                nested exception is:
                  java.net.ConnectException: Connection timed out: connect
                  at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972)
                  ....
                  ....
  </code></pre>
  
  <p>For http client I use proxy using following java code and it works perfectly:</p>
  
  <pre><code>             String proxyHost = "172.22.6.26";
               int proxyPort = 80;
               Properties systemSettings = System.getProperties();
               systemSettings.put("http.proxyPort",proxyPort);
               systemSettings.put("http.proxyHost",proxyHost);
  </code></pre>
  
  <p>I am not sure what is the exact problem but I think it should be related to proxy or firewall. I have tried several codes to use proxy in smtp client but none of them are working properly. </p>