Android Tutorial [B4X] Sending emails with Gmail REST API

Status
Not open for further replies.

Erel

Administrator
Staff member
Licensed User
This solution is compatible with B4A, B4J and B4i.

Up until now there were two ways to send emails:

1. Using the default mobile app which required the user to actually send the message (available in B4A and B4i).
2. Using SMTP from the Net library. This required the user to enter the email account details.

Both options are problematic.

Now there is a third option which is to ask the user once for permission to send emails through his gmail account:



Once allowed we can use the Gmail API to send messages.

Start with the GoogleOAuth2 tutorial: https://www.b4x.com/android/forum/threads/class-b4x-google-oauth2.79426/#content

You need to enable Gmail API in Google developers console and get a client id.

Once you configured it correctly, it is very simple to send messages:
B4X:
Dim msg As MailCreator
msg.Initialize
msg.HtmlBody = True
msg.ToList.Add("erel@basic4ppc.com")
msg.Subject = "test"
msg.Body = $"First line<br/>second line"$
Send(msg)
You can also add attachments to the email.
 

Attachments

Pencil3

Member
Licensed User
After clicking allow on the permission screen it appears the redirect goes back to the main activity. How can I have it redirect into a activity with a different name?
 

Erel

Administrator
Staff member
Licensed User
The first parameter in GoogleOAuth2 sets the target module. This is the module that will receive all events. If you want to use it from multiple activities then add a service (or use the Starter service) and from the service delegate the event to whichever activity you like.
 

Gunnar Daehling

Member
Licensed User
Hello,

this variant for sending eMails actually works great. But now I have got a problem, which I probably can not solve myself.

I want to use this code now for my Tempomaster app. The package name is:

gd.tempomaster_pro

This will give me a mistake from Google (see screenshot). I'm afraid the underscore is causing problems. Can you do something there. I can not change the package name anymore.

Best regards,
Gunnar
 

Attachments

Status
Not open for further replies.
Top