Javamail kon socket niet converteren naar TLS GMail

Ik probeer een e-mail te verzenden met JavaMail via de SMTP-server van Gmail. echter deze 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);
    }

Retourneert een deze fout

Could not convert socket to TLS;

De complete 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

Antwoord 1, autoriteit 100%

props.put("mail.smtp.ssl.trust", "smtp.gmail.com");

Antwoord 2, autoriteit 23%

Ik heb avast antivirus 10 minuten uitgeschakeld en het werkt.


Antwoord 3, autoriteit 23%

Zorg ervoor dat uw antivirusprogramma niet interfereert en voeg een uitsluiting toe aan uw firewall.


Antwoord 4, autoriteit 9%

Ik kreeg een soortgelijk probleem toen het standaard SSL-protocol voor het verzenden van e-mails was ingesteld op TLSv1 en de smtp-server dit protocol niet meer ondersteunde.
De truc was om een nieuwer protocol te gebruiken:

mail.smtp.ssl.protocols=TLSv1.2


Antwoord 5, autoriteit 8%

Het eerste antwoord van @carloswerkte voor mij:

session.getProperties().put("mail.smtp.ssl.trust", "smtp.gmail.com");

Ik heb de onderstaande eigenschap getest en werkte ook perfect voor mij:

session.getProperties().put("mail.smtp.starttls.enable", "true");

Alleen de twee eigenschappen losten dit soort problemen op, maar ik heb gegarandeerd beide gebruikt.


Antwoord 6, autoriteit 4%

In mijn geval is het probleem opgelost door verwijderende regel

prop.put("mail.smtp.starttls.enable", "true");

Misschien zijn er fouten in de SSL-configuratie op de e-mailserver, ik weet het niet zeker. Beheerders van e-mailservers geven het nooit toe en geven altijd de hostingprovider de schuld 🙂


Antwoord 7, autoriteit 4%

Na een volledige zoekopdracht, heb ik 10 minuten gehandicapt en Windows Firewall (belangrijk) en alles begon te werken!

Dit was mijn fout:

Mail server connection failed; nested exception is 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. Failed messages: 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  

Hier is het herstellen van het probleem in Avast 19.8.2393 door een uitsluiting aan SMTP-poort 587 toe te voegen (of die uw toepassing ook gebruikt):

  1. AVAST

  2. Klik op ‘Instellingen’

  3. Klik op ‘Problemen oplossen’ en vervolgens op ‘Open oude instellingen’

  1. Klik nogmaals op ‘Problemen oplossen’, scrol omlaag naar ‘Instellingen voor Redirect’ en verwijder de poort die uw app gebruikt.

In mijn geval heb ik net 587 verwijderd van SMTP-poorten.

Nu ben ik in staat om avast te gebruiken en laat mijn Windows-firewall ook ingeschakeld (niet nodig om extra uitsluiting voor de firewall toe te voegen).

Hier zijn mijn application.propertiese-maileigenschappen:

###### I am using a Google App Password which I generated in my Gmail Security settings ######
spring.mail.host = smtp.gmail.com
spring.mail.port = 587
spring.mail.protocol = smtp
spring.mail.username = gmail account
spring.mail.password = password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=5000
spring.mail.properties.mail.smtp.writetimeout=5000

Antwoord 8, autoriteit 2%

bovenstaande application.properties werkte geweldig voor mij:

spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.ssl.trust=smtp.gmail.com

Antwoord 9

Probeer de poort te wijzigen in 465

mail.smtp.socketFactory.port=465
mail.smtp.port=465

Antwoord 10

Dit kan ook zijn als de applicatie de TLS-versiedie de SMTP-host gebruikt niet ondersteunt.

Bijvoorbeeld als u probeert een SMTP-server te configureren die TLSv1.2 gebruikt zonder terugval, terwijl uw toepassing (of Java-programma dat een oudere javax.mail JAR gebruikt) slechts TLSv1.1 ondersteunt.


Antwoord 11

Als uw context Android-applicatie is,
zorg er vervolgens voor dat de tijd van uw Android-apparaat is ingesteld op de huidige datum en tijd.
De onderliggende uitzondering is:
“De SSL-certificaten werden niet geverifieerd.”


Antwoord 12

Ja, het werkt voor mij op localhost:

props.put("mail.smtp.ssl.trust", "smtp.gmail.com");

De fout wordt alleen op localhost gegenereerd, dus u moet het ook op externe servers proberen. Meestal is het bovenstaande codefragment dat goed werkt zonder de eigenschap ssl.trust


Antwoord 13

Hier is de werkende oplossing bro. Het is gegarandeerd

1) Open eerst uw gmail-account van waaruit u e-mail wilde verzenden, zoals in uw geval “”[email protected]

2) open deze link hieronder https://support.google.com/ accounts/answer/6010255?hl=nl

3) klik op ‘Ga naar het gedeelte ‘Minder veilige apps’ in Mijn account’. optie

4) Schakel het vervolgens in

5) dat is het (:

hier is mijn code

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class SendEmail {
   final String senderEmailID = "Sender Email id";
final String senderPassword = "Sender Pass word";
final String emailSMTPserver = "smtp.gmail.com";
final String emailServerPort = "465";
String receiverEmailID = null;
static String emailSubject = "Test Mail";
static String emailBody = ":)";
public SendEmail(String receiverEmailID, String emailSubject, String emailBody)
{
this.receiverEmailID=receiverEmailID;
this.emailSubject=emailSubject;
this.emailBody=emailBody;
Properties props = new Properties();
props.put("mail.smtp.user",senderEmailID);
props.put("mail.smtp.host", emailSMTPserver);
props.put("mail.smtp.port", emailServerPort);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", emailServerPort);
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
SecurityManager security = System.getSecurityManager();
try
{
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
MimeMessage msg = new MimeMessage(session);
msg.setText(emailBody);
msg.setSubject(emailSubject);
msg.setFrom(new InternetAddress(senderEmailID));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(receiverEmailID));
Transport.send(msg);
System.out.println("Message send Successfully:)");
}
catch (Exception mex)
{
mex.printStackTrace();
}
}
public class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(senderEmailID, senderPassword);
}
}
    public static void main(String[] args) {
       SendEmail mailSender;
        mailSender = new SendEmail("Receiver Email id","Testing Code 2 example","Testing Code Body yess");
    }
}

Antwoord 14

Controleer de versie van uw Javamail lib (mail.jar of javax.mail.jar). Misschien heb je een nieuwere nodig.
Download de nieuwste versie vanaf hier: https://javaee.github.io/javamail/


Antwoord 15

Probeer het SMTPSEND-programma met Javamail te gebruiken, zoals beschreven hier . Als dat op dezelfde manier faalt, is er iets mis met uw JDK-configuratie of uw netwerkconfiguratie.


Antwoord 16

Wat heeft me geholpen om dit te repareren, en ik heb alles eerder geprobeerd, was om mijn geïnstalleerde JRE naar JRE 1.8 te configureren.

Stappen in Eclipse: Windows & GT; Voorkeuren & GT; Java & GT; geïnstalleerd JRE & GT; JRE1.8.0

Als het is ingesteld op jdk, schakel dan over naar jre (wat standaard zou moeten zijn met de nieuwste Java-versie).

Other episodes