Having trouble sending to Gmail SMTP using javax.mail
<p>I want to send a mail using Gmail's SMTP server. Can you tell me why it won't connect to server when I run the bellow code.</p>

<pre><code>import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;

public class SendTrick {

  public static void main(String[] args) {
    Properties props = new Properties();
    props.put("mail.smtp.host", "465");
    props.put("mail.from", "example@gmail.com");
    props.put("mail.smtp.host", "smtp.gmail.com");
    Session session = Session.getInstance(props, null);

    try {
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom();
        msg.setRecipients(Message.RecipientType.TO,
                          "ex@gmail.com");
        msg.setSubject("JavaMail hello world example");
        msg.setText("Hello, world!\n");
        Transport.send(msg);
    } catch (MessagingException mex) {
        System.out.println("send failed, exception: " + mex);
    }
  }
} 
</code></pre>

<p>The exception in the log is</p>

<blockquote>
  <p>send failed, exception: javax.mail.MessagingException: Could not
   connect to SMTP host: smtp.gmail.com, port: 25;   nested exception is:
      java.net.ConnectException: Connection refused: connect</p>
</blockquote>
