B4J Question SMTP 'SMTPClient.UseSSL = True' error

Peter Simpson

Expert
Licensed User
Longtime User
Hiya all,
I'm using the exact same routine in an app that I'm developing in B4J as I use in B4A, but for some strange reason if I set 'SMTPClient.UseSSL = True' in B4J, I get the following error whilst trying to send email through GMail. 'SMTPClient.UseSSL = True' is needed for my GMail account and in my B4A app 'SMTPClient.UseSSL = True' works perfect and it sends email 100% with no issues or errors.
B4X:
'Code
    Dim SMTPClient As SMTP
        SMTPClient.Initialize("smtp.gmail.com", 465, "[email protected]", "xxxxxxxxxx", "SMTP") 'xxxxxxxxxx is email password to bypass 2-Step verification on my Google account
        SMTPClient.UseSSL = True '465
        SMTPClient.HtmlBody = False

        SMTPClient.Sender = "[email protected]"
        SMTPClient.To.Add("[email protected]")
        SMTPClient.Subject = "Home IP address has changed"

        SMTPClient.Body = "Your new home IP address is " = NewIP
        SMTPClient.Send
'SMTPClient.UseSSL = True' in B4J causes this error...
B4X:
sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
B4X:
Program started.
JobName = GetIP, Success = true
xxx.xxx.xx.255
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:1917)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:301)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:295)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1369)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:156)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:925)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:860)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1043)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1343)
    at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:909)
    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:231)
    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:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
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:387)
    at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
    at sun.security.validator.Validator.validate(Validator.java:260)
    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:1351)
    ... 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:145)
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:131)
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
    ... 30 more
Error sending message
sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

I have read on the internet that Java might be missing a certificate or something like that...
 
Last edited:

Peter Simpson

Expert
Licensed User
Longtime User
Hello @Erel, thank you for your help. Sorry about the delay responding to you, but I've just got back from a 31 mile bike ride.

I have jdk1.8.0_31 and jre1.8.0_31 both 64bit.
I've never seen this error before and I do not get this error in the B4A app using the exact same code with no changes.

No other B4J app that I've developed or B4J code test code that I've downloaded from this community brings up this error, only this app whilst using .UseSSL = True. I've just uninstalled and reinstalled both jdk and jre and I'm still getting the same certificate error :(

Cheers...
 
Last edited:
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Grrr, okay I've spent the last hour following instructions on the internet including running this command 'javac InstallCert.java' in the C:\Program Files\Java\jdk1.8.0_31\bin folder. Everything appears to run smoothly but no, I'm still getting the same error :mad:
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Hello @Erel,
Yes I've already tried that and I got the exact same result. I've uninstalled jdk and jre, I then manually deleted their folders, I then ran both CCleaner and MSRegClean to make sure that there was no unused registry keys. I rebooted my machine and then reinstalled both jdk and jre, I even have the JAVA_HOME settings in my Windows Environment Variables.

Everything I'm running is x64.

Thank you...
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Cheers I knew that it would work for you, I saw the sent email. I only removed the forms to see if it would make any difference.

Just out of curiosity, am I correct in saying that you're all configured for 64bit including jdk and jre, not 32bit?

Thank you @Erel
 
Last edited:
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
Hi, I have a similar problem, I can send emails using my google account all works fine, but i belive Gmail can only send 100 emails per day! I use the program to look for specific messages from paypal, then issue a code to users.

I would like to use my own domain for sending the emails, but i too need a certificate! the problem i am having is where do I get the certificate from? and what do I do with the certificate when i get one?

Do I need to buy a certificate? if so from where? my host? (I see no information about this on my host)

Thanks

Aidy
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User

Hi, Thanks I have looked at that but that all relates to gmail! i can use gmail fine, I want to use my own server!

Forget my post i have Fixed the error! by using the correct port number (25) ;)

Having said that I did buy an SSL Certificate, and install it to both java and my site. so maybe that helped? will find out i a years time!

Thanks

Aidy
 
Last edited:
Upvote 0
Top