Android Tutorial [B4X] Sending emails with Gmail REST API

Status
Not open for further replies.
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:

SS-2017-07-17_17.10.47.png


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

  • Gmail.zip
    59.7 KB · Views: 2,111

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?
 

JohnC

Expert
Licensed User
This is excellent - I needed something like this!
 

Tempomaster

Active 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

  • Screenshot_20180912-225815_Chrome.jpg
    Screenshot_20180912-225815_Chrome.jpg
    170.1 KB · Views: 662
Status
Not open for further replies.
Top