Android Tutorial [B4X] Sending emails with Gmail REST API

Discussion in 'Tutorials & Examples' started by Erel, Jul 17, 2017.

Thread Status:
Not open for further replies.
  1. Erel

    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:

    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:
    Dim msg As MailCreator
    msg.HtmlBody = 
    msg.Subject = 
    msg.Body = 
    $"First line<br/>second line"$
    You can also add attachments to the email.

    Attached Files:

  2. Pencil3

    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?
  3. Erel

    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.
  4. JohnC

    JohnC Well-Known Member Licensed User

    This is excellent - I needed something like this!
  5. Gunnar Daehling

    Gunnar Daehling Member Licensed User


    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:


    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,

    Attached Files:

  6. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this.
Thread Status:
Not open for further replies.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice