title: Could not connect to SMTP host: smtp.gmail.com, port: 587;
body: <p>I can't seem to send email using java and was hoping you guys could help.  Here's the code I'm using now (I've actually tried several examples and none of them work).</p>
  
  <pre><code>import java.util.Properties;
  import javax.mail.Message;
  import javax.mail.MessagingException;
  import javax.mail.Session;
  import javax.mail.Transport;
  import javax.mail.internet.AddressException;
  import javax.mail.internet.InternetAddress;
  import javax.mail.internet.MimeMessage;
  
  public class Mail {
  
      static Properties mailServerProperties;
      static Session getMailSession;
      static MimeMessage generateMailMessage;
  
      public static void main(String args[]) throws AddressException, MessagingException {
          generateAndSendEmail();
          System.out.println("\n\n ===&gt; Your Java Program has just sent an Email successfully. Check your email..");
      }
  
      public static void generateAndSendEmail() throws AddressException, MessagingException {
  
  //Step1     
          System.out.println("\n 1st ===&gt; setup Mail Server Properties..");
          mailServerProperties = System.getProperties();
          mailServerProperties.put("mail.smtp.port", "587"); // TLS Port
          mailServerProperties.put("mail.smtp.auth", "true"); // Enable Authentication
          mailServerProperties.put("mail.smtp.starttls.enable", "true"); // Enable StartTLS
          System.out.println("Mail Server Properties have been setup successfully..");
  
  //Step2     
          System.out.println("\n\n 2nd ===&gt; get Mail Session..");
          getMailSession = Session.getDefaultInstance(mailServerProperties, null);
          generateMailMessage = new MimeMessage(getMailSession);
          generateMailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("test@google.com"));
          generateMailMessage.addRecipient(Message.RecipientType.CC, new InternetAddress("test@google.com"));
          generateMailMessage.setSubject("Greetings from Crunchify.com..");
          String emailBody = "Test email by Crunchify.com JavaMail API example. " + "&lt;br&gt;&lt;br&gt; Regards, &lt;br&gt;Crunchify Admin";
          generateMailMessage.setContent(emailBody, "text/html");
          System.out.println("Mail Session has been created successfully..");
  
  //Step3     
          System.out.println("\n\n 3rd ===&gt; Get Session and Send mail");
          Transport transport = getMailSession.getTransport("smtp");
          // Enter your correct gmail UserID and Password
          transport.connect("smtp.gmail.com", "***my google user name ****", "**** my google password *****");
          transport.sendMessage(generateMailMessage, generateMailMessage.getAllRecipients());
          transport.close();
      }
  }
  </code></pre>
  
  <p>The error that I get from this is:</p>
  
  <pre><code>Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
    nested exception is:
      java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine
      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:275)
      at javax.mail.Service.connect(Service.java:156)
      at Mail.generateAndSendEmail(Mail.java:50)
      at Mail.main(Mail.java:21)
  Caused by: java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine
      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 java.net.Socket.connect(Unknown Source)
      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)
      ... 5 more
  </code></pre>
  
  <p>I'm successfully able to telnet into smtp.gmail.com 587 and get the response back
       220 mx.google.com ESMTP k66sm21937006yhg.39 - gsmtp</p>
  
  <p>The line of code that's having problem is: 
      transport.connect("smtp.gmail.com", "<strong><em>my google user name *</em></strong>", "<strong>** my google password <em>*</em></strong>");</p>
  
  <p>I'm replacing <strong><em>my google user name *</em></strong> and <strong>** my google password **</strong> with my google login credentials.  If anyone could help me figure this out I'd be just so happy as I've been trying to figure this out for hours.</p>