sending email with ssl using javax.mail
From reading this: http://www.oracle.com/technetwork/java/javamail/faq-135477.html#commonmistakes
The use of
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");**
and
props.put("mail.smtp.socketFactory.port", "465");
is kind of outdated. To simplify the code, use:
properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.ssl.enable", "true");
Instead of
props.put("mail.transport.protocol", "smtps");
Transport transport = session.getTransport("smtps");
Use
props.put("mail.transport.protocol", "smtp");
Transport transport =session.getTransport("smtp");
Use smtp, not smtps
I used JDK 8, Netbeans 8, JavaMail 1.5.2 and this example works fine:
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]","password");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]"));
message.setSubject("Testing Subject");
message.setText("Test Mail");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
If you are not able connect with port 465, try port 587
If you using Windows and have any Antivirus or firewall disable it and try.