Android Question Sending email

anshel1

New Member
Hello everyone,
This is my first experience in B4A.
I'm trying to write a code that sends an email.
This is the code I wrote:


Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private xui As XUI


End Sub


Sub Globals
'These global variables will be redeclared each time the activity is created.
'Private x As Int,y As Int,z As Int
Public SMTP As SMTP
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Layout")
End Sub


Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub Button1_Click
xui.MsgboxAsync("1", "B4X")
SMTP.Initialize("mail.gmail.com", 587, "[email protected]", "xxxxxxx", "SMTP")
SMTP.StartTLSMode = True
SMTP.To.Add("[email protected]")
SMTP.Subject = "asunto"
SMTP.Body = "cuerpo"
SMTP.Send
xui.MsgboxAsync("2", "B4X")
End Sub

The compilation goes through without errors but the email is not sent.
For testing, I wrote in a print line before and after sending the email. When you run the app, something strange happens: the second print happens first, followed by the first print.
Can anyone help me fix the code so that the email will be sent?

Thanks
Dov
 

Nicola Ciaramellano

Member
Licensed User
Longtime User
Hi,
the code is correct, but the email is blocked from gmail because you have to change (in your google account) the option that pemits to send email from unsecure apps.
Anyway it's not a good idea to send email with gmail using the simple SMTP protocol, because Gmail will not permit it at all in the future.
So, it's better to use GMAIL API: https://www.b4x.com/android/forum/threads/b4x-sending-emails-with-gmail-rest-api.81736/#content
or change the Email Server or, better than all, use your own email server.
Hope this helps
 
Last edited:
Upvote 0
Top