B4J Question [ABMaterial] How to send emails using SMTP and gmail?

Mashiane

Expert
Licensed User
Longtime User
Thanks, I had thought there would be a conflict of some sort. Tried smtp to send email, working properly however...

I put this in Class_Globals for my ABMaterial page.

B4X:
Private smtp As SMTP

and also added this method.

B4X:
Sub smtp_MessageSent(Success As Boolean) [the ShowToast never gets fired though clear does]
    page.IsPaused = False
    If Success = False Then
        myToastId = myToastId + 1
        page.ShowToast("toast" & myToastId, "toastred", "Email could not be sent, please try again.", 3000)
    Else
        ' clear the components
        Clear
        myToastId = myToastId + 1
        page.ShowToast("toast" & myToastId, "toastgreen", "Email sent successfully.", 3000)
    End If
End Sub

The code to process the email and send it..

B4X:
Private Sub ContactUsProcess
    'define a map to hold the form contents
    Dim m As Map
    'read the page contents to a map
    m = GetContents
    'validate the form contents where required
    If Validate(m) = False Then
        Return
    End If
    Dim fullname As String = m.get("fullname")
    Dim email As String = m.get("email")
    Dim mobilephone As String = m.get("mobilephone")
    Dim comment As String = m.get("comment")

    Try
        page.IsPaused = True [This never gets fired]
        'the form contents are ok, continue with the email send
        ' start smtp to send the emails
        smtp.Initialize("smtp.saix.net", 25, "username", "password", "smtp")
        smtp.To.Add("anele@mbangas.com")
        smtp.Body = ABMShared.Replace("FullName: " & fullname & "||Email: " & email & "||MobilePhone: " & mobilephone & "||Comment: " & comment & "||", "|", CRLF)
        smtp.Subject = "lfjss: Contact Us"
        smtp.Send
    Catch
        page.IsPaused = False
        myToastId = myToastId + 1
        page.ShowToast("toast" & myToastId, "toastred", "Email could not be sent, please try again.", 3000)
    End Try
End Sub

DoneEmail.png


My form design...

ContactUs.png


The action button will show on small devices only when this is complete.

The thing is, the toast never gets fired even when the page contents are cleared. It fires when I press the back button. How can I solve that. Also the IsPaused event does not show the spinner. Do I need to do a page refresh. Can you please advise.

Thanks
 
Upvote 0

mindful

Active Member
Licensed User
For the loading spinner you need to use page.Pause to show the spinner and page.Resume to hide the spinner. Regarding the toast i do not know ... i haven't used it yet ...
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
For the loading spinner you need to use page.Pause to show the spinner and page.Resume to hide the spinner. Regarding the toast i do not know ... i haven't used it yet ...
Thanks, fixed the spinner issue. I have decided to use my own smtp for the email sending not gmail, so that is working. The toast issue is still tricky..
 
Upvote 0
Top