javax.mail.MessagingException: не удалось преобразовать сокет в TLS; [дубликат]
При отправке почты через java я получил эту ошибку. Я добавил в свою библиотеку активацию .jar и mail.jar. но все же он показывает эту ошибку. недавно я добавил свои файлы jar в веб-контент, но переместил его из их папки lib. но все же это вызывает ошибку. это показывает эту ошибку javax.mail.MessagingException: не удалось преобразовать сокет в TLS;
Mar 16, 2021 1:13:42 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version name: Apache Tomcat/9.0.43
Mar 16, 2021 1:13:42 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Jan 28 2021 20:25:45 UTC
Mar 16, 2021 1:13:42 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version number: 9.0.43.0
Mar 16, 2021 1:13:42 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Windows 10
Mar 16, 2021 1:13:42 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 10.0
Mar 16, 2021 1:13:42 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: amd64
Mar 16, 2021 1:13:42 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: C:\Program Files\Java\jdk1.8.0_251\jre
Mar 16, 2021 1:13:42 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.8.0_251-b08
Mar 16, 2021 1:13:42 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
Mar 16, 2021 1:13:42 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: C:\Users\HP\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Mar 16, 2021 1:13:42 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: C:\Users\HP\Downloads\apache-tomcat-9.0.43\apache-tomcat-9.0.43
Mar 16, 2021 1:13:42 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\Users\HP\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Mar 16, 2021 1:13:42 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\Users\HP\Downloads\apache-tomcat-9.0.43\apache-tomcat-9.0.43
Mar 16, 2021 1:13:42 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\Users\HP\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Mar 16, 2021 1:13:42 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\Users\HP\Downloads\apache-tomcat-9.0.43\apache-tomcat-9.0.43\endorsed
Mar 16, 2021 1:13:42 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Mar 16, 2021 1:13:42 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The Apache Tomcat Native library which allows using OpenSSL was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_251\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jdk1.8.0_251/bin/../jre/bin/server;C:/Program Files/Java/jdk1.8.0_251/bin/../jre/bin;C:/Program Files/Java/jdk1.8.0_251/bin/../jre/lib/amd64;C:\Program Files\Java\jdk1.8.0_251\bin;C:\Users\HP\Downloads\apache-maven-3.6.3-bin\apache-maven-3.6.3\bin;C:\Program Files\Java\jdk1.8.0_251\bin;C:\Program Files\MySQL\MySQL Shell 8.0\bin\;C:\Users\HP\AppData\Local\Programs\Python\Python37-32\;C:\Users\HP\AppData\Local\Microsoft\WindowsApps;C:\Users\HP\AppData\Local\Programs\Python\Python37-32\Scripts\;C:\Users\HP\AppData\Local\atom\bin;C:\Users\HP\AppData\Local\JetBrains\PyCharm Community Edition 2020.2.1\bin;;C:\Users\HP\Downloads\apache-maven-3.6.3-bin\apache-maven-3.6.3\bin;C:\Users\HP\AppData\Local\Microsoft\WindowsApps;C:\Users\HP\AppData\Local\Programs\Microsoft VS Code\bin;;C:\Users\HP\Desktop\eclipse;;.]
Mar 16, 2021 1:13:42 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8085"]
Mar 16, 2021 1:13:42 PM org.apache.catalina.startup.Catalina load
INFO: Server initialization in [783] milliseconds
Mar 16, 2021 1:13:42 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Catalina]
Mar 16, 2021 1:13:42 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.43]
Mar 16, 2021 1:13:43 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Mar 16, 2021 1:13:43 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8085"]
Mar 16, 2021 1:13:43 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in [787] milliseconds
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:2155)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:752)
at javax.mail.Service.connect(Service.java:388)
at javax.mail.Service.connect(Service.java:246)
at javax.mail.Service.connect(Service.java:195)
at javax.mail.Transport.send0(Transport.java:254)
at javax.mail.Transport.send(Transport.java:124)
at org.message.sendMail(message.java:29)
at org.votingServelet.doPost(votingServelet.java:48)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.ValidationFieldVoting.doFilter(ValidationFieldVoting.java:49)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:346)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:887)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1684)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
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 sun.security.ssl.Alerts.getSSLException(Alerts.java:198)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1967)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:331)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:325)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1689)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:226)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1082)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:1010)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1079)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1388)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1416)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1400)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:626)
at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:553)
at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:2150)
... 35 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:450)
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:317)
at sun.security.validator.Validator.validate(Validator.java:262)
at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:330)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:227)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:132)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1671)
... 45 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:445)
... 51 more
Это мой класс сообщений:
package org;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.Authenticator;
public class message {
public void sendMail(String recieptient,String msg) throws MessagingException
{
Properties properties=new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
final String myAccountEmail="";
final String password="";
Session session=Session.getInstance(properties, new Authenticator(){
@Override
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(myAccountEmail,password);
}
});
Message message=prepareMessage(session,myAccountEmail,recieptient,msg);
Transport.send(message);
}
private Message prepareMessage(Session session, String myAccountEmail, String recieptient, String msg) {
// TODO Auto-generated method stub
try{
Message message=new MimeMessage(session);
message.setFrom(new InternetAddress(myAccountEmail));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recieptient));
message.setSubject("Your code for voting");
message.setText(msg);
return message;
}
catch(Exception ex)
{
}
return null;
}
}
отсюда я получаю доступ к моему классу сообщений
package org;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import conn.dataBaseConnection;
import org.message;
@WebServlet("/post3")
public class votingServelet extends HttpServlet {
public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException
{
int id=Integer.parseInt(req.getParameter("num1"));
String password=req.getParameter("num2");
ResultSet resultset=null;
String recieptent="";
String code1="";
try
{
Connection con = dataBaseConnection.initializeDatabase();
Statement stmt=con.createStatement();
String AlphaNumericString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"+"0123456789"+"abcdefghijklmnopqrstuvxyz";
StringBuilder sb = new StringBuilder(7);
for (int i = 0; i < 7; i++) {
int index = (int)(AlphaNumericString.length() * Math.random());
sb.append(AlphaNumericString.charAt(index));
}
String code=sb.toString();
String sql="UPDATE REGISTRATION SET CODE='"+code+"' where id='"+id+"'";
resultset =stmt.executeQuery("select * from registration where id='"+id+"'");
resultset.next();
recieptent=resultset.getString("email");
code=sb.toString();
stmt.executeUpdate(sql);
}
catch(Exception e)
{
System.out.print(e);
}
message msg=new message();
try {
msg.sendMail(recieptent,code1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}