B4J Question [SOLVED]Gmail not working

Zvi

Member
Licensed User
Hi, I'm trying to send a gmail through a B4J app but it's not working. I've updated my cacerts in Java and tested it with keytool and that's OK.
My code is:
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    smtp.Initialize("smtp.gmail.com", 465, "abc@gmail.com", "defgh", "SMTP")
    smtp.UseSSL = True
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub GenerateCodeBtn_MouseClicked (EventData As MouseEvent)
    smtp.To.Add("xxx@gmail.com")
    smtp.Body = "body"
    smtp.Subject = "Test"
    smtp.UseSSL = True
    smtp.StartTLSMode=True
    
    Dim trustall As CustomTrustManager
    trustall.InitializeAcceptAll
    smtp.SetCustomSSLTrustManager(trustall)
    smtp.AuthMethod = smtp.AUTH_LOGIN
    
    smtp.Send
    Log("sending message")
    
End Sub

I tried without using:
B4X:
smtp.StartTLSMode=True
and:
B4X:
Dim trustall As CustomTrustManager
    trustall.InitializeAcceptAll
    smtp.SetCustomSSLTrustManager(trustall)
    smtp.AuthMethod = smtp.AUTH_LOGIN

But got the following error:
javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:128)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:321)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:264)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:259)

when Including:
B4X:
Dim trustall As CustomTrustManager
    trustall.InitializeAcceptAll
    smtp.SetCustomSSLTrustManager(trustall)
    smtp.AuthMethod = smtp.AUTH_LOGIN

I get the error:
java.lang.RuntimeException: Empty writer returned: 530-5.7.0 Authentication Required. Learn more at
530 5.7.0 https://support.google.com/mail/?p=WantAuthError gy15-20020a05600c880f00b003f7f475c3bcsm9887218wmb.1 - gsmtp
at anywheresoftware.b4a.net.SMTPWrapper$1.run(SMTPWrapper.java:312)

and support.google.com/mail/ did not help.

when I add:
B4X:
smtp.StartTLSMode=True

I get an error:
java.net.SocketTimeoutException: Read timed out

When I use my gmail manually it works OK.
Would really appreciate some help.
 

Zvi

Member
Licensed User
Upvote 0

Zvi

Member
Licensed User
SOLVED
I reinstated 2-step-verification and made an app password for the app.
Now:
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Dim port=587 As Int
    Dim userName="xxx@gmail.com" As String
    Dim passWord="1234567890123456" As String '16-digit app password generated in Google account
    
    smtp.Initialize("smtp.gmail.com", port, userName, passWord, "smtp")
    smtp.UseSSL = True
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub GenerateCodeBtn_MouseClicked (EventData As MouseEvent)
    smtp.To.Add("yyy@gmail.com")
    smtp.Body = "body"
    smtp.Subject = "Test"
    smtp.StartTLSMode = True

    Dim trustall As CustomTrustManager
    trustall.InitializeAcceptAll
    smtp.SetCustomSSLTrustManager(trustall)
    smtp.AuthMethod = smtp.AUTH_LOGIN
    
    smtp.Send

Works perfectly!
 
Upvote 0

Zvi

Member
Licensed User
Just to clarify, the app password for Gmail is only to be used in this app. It is a 16-digit password generated by Google to replace the google email password only in this specific app.
It does not replace the email password you use to login to Gmail.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…