B4J Question SMTP 'SMTPClient.UseSSL = True' error

Firpas

Active Member
Licensed User
Longtime User
Hello everyone
I have the same problem that Peter Simpson says in the post:

https://www.b4x.com/android/forum/threads/smtp-smtpclient-usessl-true-error.50857/

I have followed the instructions of inakigarm in the post:


look at http://stackoverflow.com/questions/9644163/using-smtp-gmail-to-sending-mail

Finally I managed to import the Gmail certificate, but I still have the following error:

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:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1964)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:328)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:322)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1614)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1052)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:987)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1072)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:938)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.read(BufferedReader.java:182)
at org.apache.commons.net.io.CRLFLineReader.readLine(CRLFLineReader.java:59)
at org.apache.commons.net.smtp.SMTP.__getReply(SMTP.java:196)
at org.apache.commons.net.smtp.SMTP._connectAction_(SMTP.java:264)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:173)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:193)
at anywheresoftware.b4a.net.SMTPWrapper$1.run(SMTPWrapper.java:236)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
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:397)
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:302)
at sun.security.validator.Validator.validate(Validator.java:262)
at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1596)
... 24 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:392)
... 30 more
t

As inakigarm says, I've also tried to connect with gmail via telnet but without any result

According to Peter Simpson

Thank you @Erel for the hint(i removed my certificate just before you posted a response, sorry), and thank you @inakigarm for the link, everything is working as expected now :)

Great community…

But I have not got it yet

Any ideas?
 

Firpas

Active Member
Licensed User
Longtime User
Yes i´m running it on Windows 10

I`ve all the necessary variables in a map and here is my code:

B4X:
        Dim MailPort As Int = FM.Config.Get("MailPort")
        Dim MailHost As String = FM.Config.Get("MailHost")
        Dim MailUser As String = FM.Config.Get("MailUser")
        Dim MailPassW As String = FM.Config.Get("MailPassW")
        Dim MailAuth As String = FM.Config.Get("MailAuth")
        Dim Body As String = "Solicitud atendida: " & DateTime.Date(DateTime.Now) & " " &DateTime.Time(DateTime.Now) & CRLF
        Body = Body & RS.GetString("NOMBRE")&" "&RS.GetString("APELLIDOS") & CRLF
        Body = Body & "Los datos necesarios para su inicio de sesión son los siguientes:" & CRLF
        Body = Body & "   - Usuario: " & RS.GetString("USER") & CRLF
        Body = Body & "   - Contraseña: " & RS.GetString("PASSW") & CRLF
        Body = Body & "Guarde esta información en lugar seguro o destruyala." & CRLF
        
        Dim smtp As SMTP
              
        smtp.Initialize(MailHost, MailPort , MailUser, MailPassW, "Mailer")
        Select Case MailAuth
            Case "0"
                smtp.AuthMethod = smtp.AUTH_PLAIN ' Default
            Case "1"
                smtp.AuthMethod = smtp.AUTH_LOGIN
            Case "2"
                smtp.AuthMethod = smtp.AUTH_CRAM_MD5
        End Select
        
        If FM.Config.GetDefault("MailSSL", "") = "1" Then smtp.UseSSL = True
        If FM.Config.GetDefault("MailTLS", "") = "1" Then smtp.StartTLSMode = True
        
        smtp.Sender = "<" & MailUser & ">"
        smtp.To.Add(Email)
        smtp.Subject = "Datos de Inicio de Sesión"
        smtp.Body = Body

        smtp.Send
        
        wait for Mailer_MessageSent(Success As Boolean)
        mresp.ContentType = "application/json"
        If Success Then
            mresp.Write("Sus datos de inicio de sesión han sido enviados, revise su correo electeónico.")
        Else
            mresp.Write("Si ha olvidado o extraviado su nombre de usuario y/o su contraseña deberá ponerse en contacto con algún administrador de la aplicación.")
        End If
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Firpas

Active Member
Licensed User
Longtime User
Ok
MailHost = "smpt.gmail.com"
MaitlUser = "[email protected]"
etc, etc

all values are stored in map named Config
And the map is writen on disk (File.MapWrite(…..)
And is readed by File.ReadMap

Here is config.txt
 

Attachments

  • Config.txt
    151 bytes · Views: 272
Last edited:
Upvote 0

Firpas

Active Member
Licensed User
Longtime User
Simplifying my code I have tried this

B4X:
       Dim smtp As SMTP
        smtp.Initialize("smtp.gmail.com", 465, "[email protected]", "xxxxxx", "Mailer")
        smtp.UseSSL = True
        smtp.Sender = "<" & "[email protected]" & ">"
        smtp.To.Add(Email)
        smtp.Subject = "Test"
        smtp.Body = "Test from B4J"
       
        Dim pop As POP3
        pop.Initialize("pop.gmail.com", 995, "[email protected]", "xxxxxx", "")
        Dim trustall As CustomTrustManager
        trustall.InitializeAcceptAll
        pop.SetCustomSSLTrustManager(trustall)
       
        smtp.Send
        wait for Mailer_MessageSent(Success As Boolean)
        If Success Then
            Log("E-Mail sended ok")
        Else
            Log(LastException)
        End If

And the result is

Waiting for debugger to connect...
Program started.
java.lang.UnsatisfiedLinkError: C:\Users\Fran\AppData\Local\Temp\sqlite-3.7.2-sqlitejdbc.dll: Acceso denegado
2018-10-03 11:57:30.304:INFO::main: Logging initialized @1536ms to org.eclipse.jetty.util.log.StdErrLog
2018-10-03 11:57:30.522:INFO:eek:ejs.Server:main: jetty-9.4.z-SNAPSHOT; built: 2018-05-03T15:56:21.710Z; git: daa59876e6f384329b122929e70a80934569428c; jvm 1.8.0_181-b13
2018-10-03 11:57:30.574:INFO:eek:ejs.session:main: DefaultSessionIdManager workerName=node0
2018-10-03 11:57:30.574:INFO:eek:ejs.session:main: No SessionScavenger set, using defaults
2018-10-03 11:57:30.576:INFO:eek:ejs.session:main: node0 Scavenging every 600000ms
2018-10-03 11:57:30.582:INFO:eek:ejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@5ae63ade{/,file:///D:/Fuentes/B4J/DeporWeb/DeporSVR/Objects/www/,AVAILABLE}
2018-10-03 11:57:30.586:INFO:eek:ejs.AbstractNCSARequestLog:main: Opened D:\Fuentes\B4J\DeporWeb\DeporSVR\Objects\logs\b4j-2018_10_03.request.log
2018-10-03 11:57:30.650:INFO:eek:ejs.AbstractConnector:main: Started ServerConnector@5e853265{HTTP/1.1,[http/1.1]}{0.0.0.0:8888}
2018-10-03 11:57:30.651:INFO:eek:ejs.Server:main: Started @1883ms
Emulated network latency: 100ms
Server started
Dirección IP: 192.168.1.4
Puerto: 8888
PID: 10952
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:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1964)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:328)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:322)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1614)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1052)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:987)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1072)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:938)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.read(BufferedReader.java:182)
at org.apache.commons.net.io.CRLFLineReader.readLine(CRLFLineReader.java:59)
at org.apache.commons.net.smtp.SMTP.__getReply(SMTP.java:196)
at org.apache.commons.net.smtp.SMTP._connectAction_(SMTP.java:264)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:181)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:201)
at anywheresoftware.b4a.net.SMTPWrapper$1.run(SMTPWrapper.java:256)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
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:397)
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:302)
at sun.security.validator.Validator.validate(Validator.java:262)
at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1596)
... 24 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:392)
... 30 more
(SunCertPathBuilderException) sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
 
Last edited:
Upvote 0

Firpas

Active Member
Licensed User
Longtime User
Checked with Gmail, Hotmail, Yahoo and other servers without SSL or TLS through port 25.
Now everything works perfectly.

Again, thank you very much for your collaboration
 
Upvote 0
Top