Ich versuche, eine E-Mail mit JavaMail über den gmails SMTP-Server zu senden. jedoch dieser Code.
final String username = "[email protected]";
final String password = "mygmailpassword";
Properties props = new Properties();
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");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, 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("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
Gibt diesen Fehler zurück
Could not convert socket to TLS;
Der komplette Stacktrace
Exception in thread "main" Java.lang.RuntimeException: javax.mail.MessagingException: Could not convert socket to TLS;
nested exception is:
javax.net.ssl.SSLHandshakeException: Sun.security.validator.ValidatorException: PKIX path building failed: Sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at Test.main(Test.Java:43)
Caused by: javax.mail.MessagingException: Could not convert socket to TLS;
nested exception is:
javax.net.ssl.SSLHandshakeException: Sun.security.validator.ValidatorException: PKIX path building failed: Sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
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 Test.main(Test.Java:38)
Caused by: javax.net.ssl.SSLHandshakeException: Sun.security.validator.ValidatorException: PKIX path building failed: Sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at com.Sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.Java:174)
at com.Sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.Java:1649)
at com.Sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.Java:241)
at com.Sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.Java:235)
at com.Sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.Java:1206)
at com.Sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(ClientHandshaker.Java:136)
at com.Sun.net.ssl.internal.ssl.Handshaker.processLoop(Handshaker.Java:593)
at com.Sun.net.ssl.internal.ssl.Handshaker.process_record(Handshaker.Java:529)
at com.Sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.Java:893)
at com.Sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.Java:1138)
at com.Sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.Java:1165)
at com.Sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.Java:1149)
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)
... 7 more
Caused by: Sun.security.validator.ValidatorException: PKIX path building failed: Sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at Sun.security.validator.PKIXValidator.doBuild(PKIXValidator.Java:323)
at Sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.Java:217)
at Sun.security.validator.Validator.validate(Validator.Java:218)
at com.Sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.Java:126)
at com.Sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.Java:209)
at com.Sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.Java:249)
at com.Sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.Java:1185)
... 17 more
Caused by: Sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at Sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.Java:174)
at Java.security.cert.CertPathBuilder.build(CertPathBuilder.Java:238)
at Sun.security.validator.PKIXValidator.doBuild(PKIXValidator.Java:318)
... 23 more
props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
Stellen Sie sicher, dass Ihr Antivirenprogramm nicht stört, und fügen Sie Ihrer Firewall einen Ausschluss hinzu.
Ich habe Avast Antivirus für 10 Minuten deaktiviert und es funktioniert.
Die erste Antwort von @ carlos hat bei mir funktioniert:
session.getProperties().put("mail.smtp.ssl.trust", "smtp.gmail.com");
Ich habe die unten stehende Immobilie getestet und auch für mich perfekt gearbeitet:
session.getProperties().put("mail.smtp.starttls.enable", "true");
Die beiden Eigenschaften allein haben diese Art von Problem gelöst, aber ich habe beide aus Sicherheitsgründen verwendet.
Wenn Ihr Kontext Android Anwendung ist, stellen Sie sicher, dass Ihre Android Gerätezeit auf das aktuelle Datum und die aktuelle Uhrzeit eingestellt ist. Die zugrunde liegende Ausnahme ist "Die SSL-Zertifikate wurden nicht" authentifiziert werden. "
Was mir geholfen hat, dieses Problem zu beheben, und ich habe alles zuvor versucht, war die Konfiguration meiner installierten JRE auf JRE 1.8.
Schritte in Eclipse: Windows> Einstellungen> Java> Installiertes JRE> jre1.8.0
Wenn es auf jdk gesetzt ist, wechseln Sie zu jre (was standardmäßig mit der neuesten Java version) festgelegt werden soll).
Dies kann auch der Fall sein, wenn die Anwendung TLS-Version nicht unterstützt, den der SMTP-Host verwendet.
Wenn Sie beispielsweise versuchen, einen SMTP-Server zu konfigurieren, der TLSv1.2 ohne Fallback verwendet, unterstützt Ihre Anwendung (oder Java ein älteres javax.mail-JAR) nur TLSv1.1.
Versuchen Sie, den Port auf 465 zu ändern
mail.smtp.socketFactory.port=465
mail.smtp.port=465
In meinem Fall wurde das Problem durch Löschen der Zeile gelöst
prop.put("mail.smtp.starttls.enable", "true");
Es kann an einigen SSL-Konfigurationsfehlern auf dem E-Mail-Server liegen, ich bin mir nicht sicher. E-Mail-Server-Administratoren geben das nie zu und beschuldigen immer den Hosting-Provider :)