2
Email email = new SimpleEmail();
String authuser = "......@gmail.com";
String authpwd = "*******";
// Very Important, Don't use email.setAuthentication()
email.setSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd));
email.setDebug(true); // true if you want to debug
email.setHostName("smtp.gmail.com");

email.getMailSession().getProperties().put("mail.smtp.auth", "true");
email.getMailSession().getProperties().put("mail.debug", "true");
email.getMailSession().getProperties().put("mail.smtp.port", "587");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.port", "587");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.class",   "javax.net.ssl.SSLSocketFactory");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.fallback", "false");
email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
email.setFrom("........@gmail.com", "SenderName");
email.setSubject("TestMail");
email.setMsg("This is a test mail?");
email.addTo(".............@gmail.com", "ToName");
email.setTLS(true);
email.send();
skaffman
  • 398,947
  • 96
  • 818
  • 769

1 Answers1

0

Get rid of all email.getMailSession().getProperties() - they are not required and may be polluting the setup.

Then remove the line that sets the port. (email.setSmtpPort(587);)

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • I removed all those email.getMailSession().getProperties() but still getting the error STARTTLS command. Also did setSSL(true) then getting the error class javax.mail.MessagingException:Could not connect to SMTP host: smtp.gmail.com, port: 465; nested exception is: java.net.SocketException: Cannot find the specified class java.security.PrivilegedActionException: java.lang.ClassNotFoundException: com.ibm.websphere.ssl.protocol.SSLSocketFactory – Chetan Sharma Jan 25 '10 at 14:34