I have looked at the other similar questions which recommended adding properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");
which still didn't work for me. I also saw some answers recommending commenting out properties.put("mail.smtp.starttls.enable", "true");
which also didn't work for me. I have no antivirus other than the default windows one and no other special program; why is this happening?
Code
public static void sendMail(String recipient) throws MessagingException {
System.out.println("Preparing to send email...");
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.debug", "true");
properties.put("mail.smtp.port", "587");
String myAccountEmail = "[email protected]";
String myAccountPassword = "alexIsFarting!";
Session session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(myAccountEmail, myAccountPassword);
}
});
Message message = prepareMessage(session, myAccountEmail, recipient);
Transport.send(message);
System.out.println("Email sent!");
}
private static Message prepareMessage(Session session, String myAccountEmail, String recipient) {
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(myAccountEmail));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
message.setSubject("Hello, this is a test email!");
message.setText("Big strong men");
return message;
} catch (Exception ex) {
Logger.getLogger(SendMail.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
Full Error
Exception in thread "main" javax.mail.MessagingException: Could not convert socket to TLS;
nested exception is:
javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1907)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:666)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)
at SendMail.sendMail(SendMail.java:33)
at Main.main(Main.java:5)
Caused by: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
at java.base/sun.security.ssl.HandshakeContext.<init>(HandshakeContext.java:172)
at java.base/sun.security.ssl.ClientHandshakeContext.<init>(ClientHandshakeContext.java:103)
at java.base/sun.security.ssl.TransportContext.kickstart(TransportContext.java:239)
at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:443)
at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:421)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:527)
at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:464)
at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1902)
... 8 more
CodePudding user response:
I have found the solution, adding this line properties.put("mail.smtp.ssl.protocols", "TLSv1.2"
to the properties, as well as creating an app password for the google account you are using fixes it. You need to enable 2FA on the account and then use the created password in your program
Complete final properties
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.starttls.required", "true");
properties.put("mail.smtp.ssl.protocols", "TLSv1.2");