8

I have a Problem sending Mails with Javax. We used gmail for about 4 years to send mails from our Java Software. Now i get the following Error:

Exception in thread "main" java.lang.RuntimeException: 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 SendEmail.sendMissingMailWeek(SendEmail.java:233)
    at main.negVerkaeufeMailenWeek(main.java:368)
    at main.main(main.java:79)
Caused by: 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:194)
    at javax.mail.Transport.send(Transport.java:124)
    at SendEmail.sendMissingMailWeek(SendEmail.java:226)
    ... 2 more
Caused by: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
    at sun.security.ssl.HandshakeContext.<init>(HandshakeContext.java:171)
    at sun.security.ssl.ClientHandshakeContext.<init>(ClientHandshakeContext.java:98)
    at sun.security.ssl.TransportContext.kickstart(TransportContext.java:220)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:428)
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:549)
    at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:486)
    at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1902)
    ... 9 more

Process finished with exit code 1

These are my Settings for gmail:

            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.port", "587");
            props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
            props.put("mail.smtp.debug", "true");

I also tried other Mailserver (O365, Strato) but got the same Error.

Wenfang Du
  • 8,804
  • 9
  • 59
  • 90
Fabian Schmitz
  • 81
  • 1
  • 1
  • 3
  • Not sure if this will help but try passing `-Djdk.tls.client.protocols=TLSv1.2` and `-Djavax.net.debug=all` might reveal more – g00se Jul 30 '21 at 12:49
  • Same issue with my client app here. Was working a few weeks ago, now it's throwing an error. – ryvantage Aug 11 '21 at 02:58
  • @ryvantage Have you tried providing protocol information in your mail configuration? I mean, please, try including the following code in your mail `props`: `props.put("mail.smtp.ssl.protocols", "TLSv1.2")`. In addition, try tweaking ports `465` and `587`, and the configuration property `mail.smtp.starttls.required`. I hope it helps. – jccampanero Aug 11 '21 at 13:51
  • Please, consider read [this related](https://groups.google.com/g/dspace-tech/c/clqlwv3r_zY) google groups article, it can be of help as well. – jccampanero Aug 11 '21 at 16:14
  • 1
    Please, can you provide more information about your jdk and java mail versions? I tested email integration with your configuration and it worked properly. Maybe, the problem can be related to the environment in which your client run? – jccampanero Aug 11 '21 at 21:43
  • Possibly related to https://stackoverflow.com/q/43143884/14765128 – thehale Aug 11 '21 at 23:35

2 Answers2

19

The error indicates some problem related to the TLS protocol required for establishing a secure connection with the mail servers.

As you can see in the questions comments, the problem can be caused by a lot of different things. Without knowing your OS, JDK and JavaMail library versions we can only guest the true reason.

@g00se gives you a good advice: please, try running your program with SSL debugging enabled by providing the following option:

-Djavax.net.debug=all

In addition, try including explicitly the TLS protocol in your configuration properties, it may be of help:

props.put("mail.smtp.ssl.protocols", "TLSv1.2");

In any way, for the symptoms you indicated, and due to the fact that AFAIK Google has not changed any related configuration recently, probably your problem will be related with a change introduced in the JDK distributions in order to disable by default insecure TLS protocol versions:

security-libs/javax.net.ssl
Disable TLS 1.0 and 1.1

TLS 1.0 and 1.1 are versions of the TLS protocol that are no longer considered secure and have been superseded by more secure and modern versions (TLS 1.2 and 1.3).

These versions have now been disabled by default. If you encounter issues, you can, at your own risk, re-enable the versions by removing "TLSv1" and/or "TLSv1.1" from the jdk.tls.disabledAlgorithms security property in the java.security configuration file.

As you can see in the bug description, the change has been backported to different JDK versions.

Perhaps you upgraded your JDK and the problem raised.

If that is the case, as indicated in the quote, try editing your java.security configuration file and remove TLSv1 and/or TLSv1.1 from the jdk.tls.disabledAlgorithms security property.

jccampanero
  • 50,989
  • 3
  • 20
  • 49
  • The change in JDK was the problem. Using `props.put("mail.smtp.ssl.protocols", "TLSv1.2");` worked. – ryvantage Aug 13 '21 at 14:50
  • That is great @ryvantage!! I am very happy to hear that you were able to solve the problem and that the answer was helpful. Thank you very much. – jccampanero Aug 13 '21 at 15:00
  • 1
    For my, a change from AdoptOpenJDK 15 to Zulu JDK 17 triggered the problem. Using `props.put("mail.smtp.ssl.protocols", "TLSv1.2");` also solved it for me. – Devabc Sep 19 '21 at 21:03
  • If you are working on an old java version or servlet use "TLSv1.2" – adnan ahmed Dec 06 '21 at 12:47
6

I found an article with the same error message that is not posted yet:

postfix and openJDK 11: "No appropriate protocol (protocol is disabled or cipher suites are inappropriate)"

The answer there is:

After upgrade JavaMail from 1.4.7 to 1.6.2 the error is gone!

Sasori
  • 73
  • 5