B4J Question SMTP 'SMTPClient.UseSSL = True' error

Discussion in 'B4J Questions' started by Peter Simpson, Feb 18, 2015.

  1. Peter Simpson

    Peter Simpson Expert Licensed 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.
    Code:
    'Code
        Dim SMTPClient As SMTP
            SMTPClient.Initialize(
    "smtp.gmail.com"465"xxxxxxxxxx@gmail.com""xxxxxxxxxx""SMTP"'xxxxxxxxxx is email password to bypass 2-Step verification on my Google account
            SMTPClient.UseSSL = True '465
            SMTPClient.HtmlBody = False

            SMTPClient.Sender = 
    "sendersemail@email.com"
            SMTPClient.To.Add(
    "recipient@email.com")
            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...
    Code:
    sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    Code:
    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: Feb 18, 2015
  2. Erel

    Erel Administrator Staff Member Licensed User

    Seems like your Java runtime is missing the certificate or certificate authority. Which version of Java are you running? On Windows?
     
  3. Peter Simpson

    Peter Simpson Expert Licensed 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: Feb 18, 2015
  4. Peter Simpson

    Peter Simpson Expert Licensed 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:
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Have you tried to use StartTLSMode instead of UseSSL? This is the more common mode.

    You will need to switch to port 587.
     
  6. Peter Simpson

    Peter Simpson Expert Licensed 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...
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    It works fine here. You probably received an email. You need to find how to add Gmail certificate to your computer.

    Note that if it is meant to be a non-ui app then it is better to create a real non-ui app instead of a ui app without any form.
     
  8. inakigarm

    inakigarm Well-Known Member Licensed User

    Peter Simpson likes this.
  9. Peter Simpson

    Peter Simpson Expert Licensed 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: Feb 19, 2015
  10. Erel

    Erel Administrator Staff Member Licensed User

    It is not related to your certificate. The JRE trust store doesn't recognize Google's certificate.

    The jdk version is not relevant either.
     
    Peter Simpson likes this.
  11. Peter Simpson

    Peter Simpson Expert Licensed User

    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...
     
  12. aidymp

    aidymp Active Member Licensed 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
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    Can you post the error message that you are seeing?
     
  14. Peter Simpson

    Peter Simpson Expert Licensed User

  15. aidymp

    aidymp Active Member Licensed 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: Mar 10, 2015
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice