title: Java Send mail failure, gmail with SMTP
body: <p>I have written java code for send mail which is giving exception when i use port number 587  and is still running without stoping when i use port 465.
  I use Tomcat, and I do not know if there is a configuration to make.
  Mail.java  </p>
  
  <pre><code>public class Mail {
          String to;
          String from;
          String message;
          String subject;
          String smtpServ;
          //getter and setter
          public int sendMail(){  
              try
              {
                  Properties props = System.getProperties();
                  props.put("mail.transport.protocol", "smtp" );
                  props.put("mail.smtp.starttls.enable","true" );
                  props.put("mail.smtp.host","smtp.gmail.com");
                  props.put("mail.smtp.auth", "true" );
                  props.put("mail.smtp.port", "456");
                  Authenticator auth = new SMTPAuthenticator();
                  Session session = Session.getInstance(props, auth);
                  Message msg = new MimeMessage(session);
                  msg.setFrom(new InternetAddress(from));
                  msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to,false));
                  msg.setSubject(subject);
                  msg.setText(message);
                  msg.setHeader("MyMail", "Mr. XYZ" );
                  msg.setSentDate(new Date());
                  Transport.send(msg);
                  System.out.println("Message sent to"+to+" OK." );
                  return 0;
              }
              catch (Exception ex)
              {
                ex.printStackTrace();
                System.out.println("Exception "+ex);
                return -1;
              }
        }
     }
  
  
  public class SMTPAuthenticator extends javax.mail.Authenticator {
          @Override
          public PasswordAuthentication getPasswordAuthentication() {
              String username =  "***@gmail.com";           // specify your email id here (sender's email id)
              String password = "***";                                      // specify your password here
              return new PasswordAuthentication(username, password);
          }
    }
  </code></pre>
  
  <p>index.jsp: the </p>
  
  <pre><code>&lt;body&gt;
  &lt;form action="sendMail.jsp" method="POST"&gt;
  &lt;input type="text" name="to" value="" /&gt;
  &lt;input type="text" name="subject" value="" /&gt;
  &lt;textarea name="message" rows="8" cols="30"&gt;
  &lt;/textarea&gt;
  &lt;input type="submit" value="Send Mail" /&gt;
  </code></pre>
  
  <p>sendMail.jsp</p>
  
  <pre><code>&lt;jsp:useBean id="mail" scope="session" class="mailing.Mail" /&gt;
  &lt;jsp:setProperty name="mail" property="to" param="to" /&gt;
  &lt;jsp:setProperty name="mail" property="from" value="****@gmail.com" /&gt;
  &lt;jsp:setProperty name="mail" property="smtpServ" value="smtp.gmail.com" /&gt;
  &lt;jsp:setProperty name="mail" property="subject" param="subject" /&gt;
  &lt;jsp:setProperty name="mail" property="message" param="message" /&gt;
  &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  ...
  &lt;body&gt;
  &lt;%
  String to = mail.getTo();
  int result;
  result = mail.sendMail();
  }  
  %&gt;
  </code></pre>