email - JavaMail and Gmail: first attempt results in "Remote host closed connection during handshake" but after next attempt everything works -


i trying send emails using java web application. use java 8, spring 4 , wildfly 9.2 server app. environment set on local machine. problem when run code first time, returns following exception:

javax.mail.messagingexception: not convert socket tls; nested exception is: javax.net.ssl.sslhandshakeexception: remote host closed connection during handshake

but next attempts result in success, email sent. use following code:

try {     string user = "user";     string password = "password";     string emailaddress = "to@gmail.com"     properties emailproperties = new properties();     emailproperties.setproperty("mail.smtp.host", "smtp.gmail.com");     emailproperties.setproperty("mail.smtp.port", "587");     emailproperties.setproperty("mail.smtp.starttls.enable", "true");     emailproperties.setproperty("mail.smtp.debug", "true");     emailproperties.setproperty("mail.smtp.auth", "true");     emailproperties.setproperty("mail.smtp.ssl.trust", "smtp.gmail.com");      authenticator auth = new authenticator() {         protected passwordauthentication getpasswordauthentication() {             return new passwordauthentication(user, password);         }     };      session session = session.getinstance(emailproperties, auth);     session.setdebug(true);      mimemessage email = new mimemessage(session);     email.setfrom(new internetaddress(user));     email.addrecipient(javax.mail.message.recipienttype.to, new internetaddress(emailaddress));     email.setsubject("subject");     email.setcontent("content", "text/html");     transport.send(email); } catch (messagingexception e) {     log.error("email not sent: {}", e.getmessage()); } 

do know why may happen? debug log first attempt:

12:49:32,981 debug: getprovider() returning javax.mail.provider[transport,smtp,com.sun.mail.smtp.smtptransport,oracle,1.5.3] 12:49:33,078 debug smtp: need username , password authentication 12:49:33,078 debug smtp: useehlo true, useauth true 12:49:33,079 debug smtp: trying connect host "smtp.gmail.com", port 587, isssl false 12:49:33,159 220 smtp.gmail.com esmtp gk6sm22113963wjc.21 - gsmtp 12:49:33,159 debug smtp: connected host "smtp.gmail.com", port: 587 12:49:33,159  12:49:43,166 ehlo 192.168.0.104 12:49:43,200 250-smtp.gmail.com @ service, [178.235.6.245] 12:49:43,200 250-size 35882577 12:49:43,200 250-8bitmime 12:49:43,200 250-starttls 12:49:43,200 250-enhancedstatuscodes 12:49:43,201 250-pipelining 12:49:43,201 250-chunking 12:49:43,201 250 smtputf8 12:49:43,201 debug smtp: found extension "size", arg "35882577" 12:49:43,201 debug smtp: found extension "8bitmime", arg "" 12:49:43,201 debug smtp: found extension "starttls", arg "" 12:49:43,202 debug smtp: found extension "enhancedstatuscodes", arg "" 12:49:43,202 debug smtp: found extension "pipelining", arg "" 12:49:43,202 debug smtp: found extension "chunking", arg "" 12:49:43,202 debug smtp: found extension "smtputf8", arg "" 12:49:43,202 starttls 12:49:43,245 220 2.0.0 ready start tls 12:50:35,603 error c.f.s.services.utility.emailsender - email not sent: not convert socket tls 

and debug log next attempts:

13:17:19,466 debug: getprovider() returning javax.mail.provider[transport,smtp,com.sun.mail.smtp.smtptransport,oracle,1.5.3] 13:17:19,489 debug smtp: need username , password authentication 13:17:19,489 debug smtp: useehlo true, useauth true 13:17:19,489 debug smtp: trying connect host "smtp.gmail.com", port 587, isssl false 13:17:19,556 220 smtp.gmail.com esmtp 18sm29785121wmr.5 - gsmtp 13:17:19,556 debug smtp: connected host "smtp.gmail.com", port: 587 13:17:19,556  13:17:29,621 ehlo 192.168.0.104 13:17:29,664 250-smtp.gmail.com @ service, [178.235.6.245] 13:17:29,664 250-size 35882577 13:17:29,665 250-8bitmime 13:17:29,665 250-starttls 13:17:29,665 250-enhancedstatuscodes 13:17:29,665 250-pipelining 13:17:29,665 250-chunking 13:17:29,665 250 smtputf8 13:17:29,665 debug smtp: found extension "size", arg "35882577" 13:17:29,665 debug smtp: found extension "8bitmime", arg "" 13:17:29,665 debug smtp: found extension "starttls", arg "" 13:17:29,666 debug smtp: found extension "enhancedstatuscodes", arg "" 13:17:29,666 debug smtp: found extension "pipelining", arg "" 13:17:29,666 debug smtp: found extension "chunking", arg "" 13:17:29,666 debug smtp: found extension "smtputf8", arg "" 13:17:29,666 starttls 13:17:29,714 220 2.0.0 ready start tls 13:17:29,891 ehlo 192.168.0.104 13:17:29,927 250-smtp.gmail.com @ service, [178.235.6.245] 13:17:29,927 250-size 35882577 13:17:29,927 250-8bitmime 13:17:29,927 250-auth login plain xoauth2 plain-clienttoken oauthbearer xoauth 13:17:29,927 250-enhancedstatuscodes 13:17:29,927 250-pipelining 13:17:29,927 250-chunking 13:17:29,927 250 smtputf8 13:17:29,928 debug smtp: found extension "size", arg "35882577" 13:17:29,928 debug smtp: found extension "8bitmime", arg "" 13:17:29,928 debug smtp: found extension "auth", arg "login plain xoauth2 plain-clienttoken oauthbearer xoauth" 13:17:29,928 debug smtp: found extension "enhancedstatuscodes", arg "" 13:17:29,928 debug smtp: found extension "pipelining", arg "" 13:17:29,928 debug smtp: found extension "chunking", arg "" 13:17:29,928 debug smtp: found extension "smtputf8", arg "" 13:17:29,928 debug smtp: attempt authenticate using mechanisms: login plain digest-md5 ntlm  13:17:29,928 debug smtp: auth login command trace suppressed 13:17:30,220 debug smtp: auth login succeeded 13:17:30,221 debug smtp: use8bit false 13:17:30,221 mail from:<from@gmail.com> 13:17:30,255 250 2.1.0 ok 18sm29785121wmr.5 - gsmtp 13:17:30,255 rcpt to:<to@gmail.com> 13:17:30,285 250 2.1.5 ok 18sm29785121wmr.5 - gsmtp 13:17:30,285 debug smtp: verified addresses 13:17:30,285 debug smtp:   to@gmail.com 13:17:30,285 data 13:17:30,455 354  go ahead 18sm29785121wmr.5 - gsmtp 13:17:30,455 from: from@gmail.com 13:17:30,455 to: to@gmail.com 13:17:30,456 message-id: <16429346.2.1479039439466@mac-mini-jacek.local> 13:17:30,456 subject: subject 13:17:30,456 mime-version: 1.0 13:17:30,456 content-type: text/html; charset=us-ascii 13:17:30,456 content-transfer-encoding: 7bit 13:17:30,456  13:17:30,456 content 13:17:30,456 . 13:17:30,958 250 2.0.0 ok 1479039450 18sm29785121wmr.5 - gsmtp 13:17:30,958 debug smtp: message delivered mail server 13:17:30,958 quit 13:17:30,993 221 2.0.0 closing connection 18sm29785121wmr.5 - gsmtp 

update

after adding javax.net.debug=ssl see during first attempt following lines:

21:04:29,655 info truststore is: /library/java/javavirtualmachines/jdk1.8.0_91.jdk/contents/home/jre/lib/security/cacerts 21:04:29,656 truststore type : jks 21:04:29,656 truststore provider :  21:04:29,656 init truststore 21:04:29,727 adding trusted cert: long list of "adding trusted cert:" 21:04:29,794 trigger seeding of securerandom 21:04:29,794 done seeding securerandom 21:04:39,903 ignoring unavailable cipher suite: tls_dhe_dss_with_aes_256_gcm_sha384 21:04:39,935 ignoring unavailable cipher suite: tls_rsa_with_aes_256_cbc_sha 21:04:39,997 ignoring unavailable cipher suite: tls_dhe_rsa_with_aes_256_gcm_sha384 21:04:39,997 ignoring unavailable cipher suite: tls_ecdh_ecdsa_with_aes_256_cbc_sha 21:04:39,997 ignoring unavailable cipher suite: tls_ecdhe_ecdsa_with_aes_256_gcm_sha384 21:04:39,997 ignoring unavailable cipher suite: tls_rsa_with_aes_256_cbc_sha256 21:04:39,997 ignoring unavailable cipher suite: tls_dhe_dss_with_aes_256_cbc_sha 21:04:39,997 ignoring unavailable cipher suite: tls_ecdh_ecdsa_with_aes_256_gcm_sha384 21:04:39,997 ignoring unavailable cipher suite: tls_ecdh_rsa_with_aes_256_cbc_sha384 21:04:39,997 ignoring unavailable cipher suite: tls_rsa_with_aes_256_gcm_sha384 21:04:39,997 ignoring unavailable cipher suite: tls_ecdh_ecdsa_with_aes_256_cbc_sha384 21:04:39,997 ignoring unavailable cipher suite: tls_ecdhe_rsa_with_aes_256_cbc_sha384 21:04:39,997 ignoring unavailable cipher suite: tls_ecdh_rsa_with_aes_256_cbc_sha 21:04:39,997 ignoring unavailable cipher suite: tls_ecdh_rsa_with_aes_256_gcm_sha384 21:04:39,998 ignoring unavailable cipher suite: tls_ecdhe_ecdsa_with_aes_256_cbc_sha384 21:04:39,998 ignoring unavailable cipher suite: tls_dhe_rsa_with_aes_256_cbc_sha256 21:04:39,998 ignoring unavailable cipher suite: tls_ecdhe_ecdsa_with_aes_256_cbc_sha 21:04:39,998 ignoring unavailable cipher suite: tls_dhe_dss_with_aes_256_cbc_sha256 21:04:39,998 ignoring unavailable cipher suite: tls_dhe_rsa_with_aes_256_cbc_sha 21:04:39,998 ignoring unavailable cipher suite: tls_ecdhe_rsa_with_aes_256_cbc_sha 21:04:39,998 ignoring unavailable cipher suite: tls_ecdhe_rsa_with_aes_256_gcm_sha384 21:04:40,007 allow unsafe renegotiation: false 21:04:40,007 allow legacy hello messages: true 21:04:40,007 initial handshake: true 21:04:40,007 secure renegotiation: false 21:04:40,007 ignoring unsupported cipher suite: tls_ecdhe_ecdsa_with_aes_128_cbc_sha256 tlsv1 21:04:40,008 ignoring unsupported cipher suite: tls_ecdhe_rsa_with_aes_128_cbc_sha256 tlsv1 21:04:40,008 ignoring unsupported cipher suite: tls_rsa_with_aes_128_cbc_sha256 tlsv1 21:04:40,008 ignoring unsupported cipher suite: tls_ecdh_ecdsa_with_aes_128_cbc_sha256 tlsv1 21:04:40,008 ignoring unsupported cipher suite: tls_ecdh_rsa_with_aes_128_cbc_sha256 tlsv1 21:04:40,008 ignoring unsupported cipher suite: tls_dhe_rsa_with_aes_128_cbc_sha256 tlsv1 21:04:40,008 ignoring unsupported cipher suite: tls_dhe_dss_with_aes_128_cbc_sha256 tlsv1 21:04:40,008 ignoring unsupported cipher suite: tls_ecdhe_ecdsa_with_aes_128_cbc_sha256 tlsv1.1 21:04:40,008 ignoring unsupported cipher suite: tls_ecdhe_rsa_with_aes_128_cbc_sha256 tlsv1.1 21:04:40,008 ignoring unsupported cipher suite: tls_rsa_with_aes_128_cbc_sha256 tlsv1.1 21:04:40,008 ignoring unsupported cipher suite: tls_ecdh_ecdsa_with_aes_128_cbc_sha256 tlsv1.1 21:04:40,008 ignoring unsupported cipher suite: tls_ecdh_rsa_with_aes_128_cbc_sha256 tlsv1.1 21:04:40,008 ignoring unsupported cipher suite: tls_dhe_rsa_with_aes_128_cbc_sha256 tlsv1.1 21:04:40,009 ignoring unsupported cipher suite: tls_dhe_dss_with_aes_128_cbc_sha256 tlsv1.1 21:04:40,011 %% no cached client session 21:04:40,014 *** clienthello, tlsv1.2 21:04:40,015 randomcookie:  gmt: 1479260888 bytes = { 36, 201, 224, 18, 161, 111, 128, 63, 52, 24, 49, 83, 212, 131, 233, 146, 53, 84, 44, 138, 116, 131, 28, 55, 75, 36, 44, 170 } 21:04:40,015 session id:  {} 21:04:40,016 cipher suites: [tls_ecdhe_ecdsa_with_aes_128_cbc_sha256, tls_ecdhe_rsa_with_aes_128_cbc_sha256, tls_rsa_with_aes_128_cbc_sha256, tls_ecdh_ecdsa_with_aes_128_cbc_sha256, tls_ecdh_rsa_with_aes_128_cbc_sha256, tls_dhe_rsa_with_aes_128_cbc_sha256, tls_dhe_dss_with_aes_128_cbc_sha256, tls_ecdhe_ecdsa_with_aes_128_cbc_sha, tls_ecdhe_rsa_with_aes_128_cbc_sha, tls_rsa_with_aes_128_cbc_sha, tls_ecdh_ecdsa_with_aes_128_cbc_sha, tls_ecdh_rsa_with_aes_128_cbc_sha, tls_dhe_rsa_with_aes_128_cbc_sha, tls_dhe_dss_with_aes_128_cbc_sha, tls_ecdhe_ecdsa_with_aes_128_gcm_sha256, tls_ecdhe_rsa_with_aes_128_gcm_sha256, tls_rsa_with_aes_128_gcm_sha256, tls_ecdh_ecdsa_with_aes_128_gcm_sha256, tls_ecdh_rsa_with_aes_128_gcm_sha256, tls_dhe_rsa_with_aes_128_gcm_sha256, tls_dhe_dss_with_aes_128_gcm_sha256, tls_ecdhe_ecdsa_with_3des_ede_cbc_sha, tls_ecdhe_rsa_with_3des_ede_cbc_sha, ssl_rsa_with_3des_ede_cbc_sha, tls_ecdh_ecdsa_with_3des_ede_cbc_sha, tls_ecdh_rsa_with_3des_ede_cbc_sha, ssl_dhe_rsa_with_3des_ede_cbc_sha, ssl_dhe_dss_with_3des_ede_cbc_sha, tls_empty_renegotiation_info_scsv] 21:04:40,017 compression methods:  { 0 } 21:04:40,018 extension elliptic_curves, curve names: {secp256r1, sect163k1, sect163r2, secp192r1, secp224r1, sect233k1, sect233r1, sect283k1, sect283r1, secp384r1, sect409k1, sect409r1, secp521r1, sect571k1, sect571r1, secp160k1, secp160r1, secp160r2, sect163r1, secp192k1, sect193r1, sect193r2, secp224k1, sect239k1, secp256k1} 21:04:40,018 extension ec_point_formats, formats: [uncompressed] 21:04:40,019 extension signature_algorithms, signature_algorithms: sha512withecdsa, sha512withrsa, sha384withecdsa, sha384withrsa, sha256withecdsa, sha256withrsa, sha224withecdsa, sha224withrsa, sha1withecdsa, sha1withrsa, sha1withdsa 21:04:40,019 extension server_name, server_name: [type=host_name (0), value=smtp.gmail.com] 21:04:40,019 *** 21:04:40,019 default task-1, write: tlsv1.2 handshake, length = 216 21:04:40,020 default task-1, received eofexception: error 21:04:40,020 default task-1, handling exception: javax.net.ssl.sslhandshakeexception: remote host closed connection during handshake 21:04:40,022 default task-1, send tlsv1.2 alert:  fatal, description = handshake_failure 21:04:40,022 default task-1, write: tlsv1.2 alert, length = 2 21:04:40,022 default task-1, called closesocket() 

and every next attempt has this:

21:20:03,114 default task-2, write: tlsv1.2 handshake, length = 216 21:20:03,149 default task-2, read: tlsv1.2 handshake, length = 91 21:20:03,150 *** serverhello, tlsv1.2 many other lines 


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -