Sending an email via the Gmail API in Android is a breeze


Here we'll show you how to use our Android SDK to send an email via the Gmail API.

Get Set Up

1 Log in to Temboo. If you don't already have an account, you can register for free.

2Download the Temboo Android SDK and add it to your development environment. Confirm that your project has <uses-permission android:name="android.permission.INTERNET"/> in its manifest. Clear the text in the generated TextArea, and set its id to @+id/result. If you need help with any of these steps, please refer to our Android getting started tutorial.

3You'll also need a Gmail account, which you can create here. To authenticate with Google, you'll want to enable 2-Step Verification and generate an App Password for Temboo.

4Sign in to your Google Account settings page by clicking on your name or picture in the upper right corner of the screen and then clicking Account.

5Scroll down to the "Signing in" box.

6Click 2-Step Verification. This will bring you to the 2-Step Verification settings page.

7You will then see a step-by-step guide which will guide you through the setup process.

8After you've enabled 2-Step Verification, you'll be prompted to create an App Password.

9In the Select app dropdown menu, choose "Other", and give this app a name (e.g., TembooApp).

10Click "Generate". You'll be given a 16-digit passcode that can be used to access your Google Account from Temboo.

Note: If you wish to authenticate with OAuth credentials, you should use the Google > Gmailv2 Choreos.

11 Go to the Google > Gmail > SendEmail Choreo in our Library.

12Fill in your Gmail username (email address) and App-specific password, then hit the Save Profile button to save a Profile for your Gmail account. Now you can reuse your Gmail authentication details on any of our Gmail Choreos and it will also make your Android code simpler and more secure.

Test the Choreo

13Now let's test the Google > Gmail > SendEmail Choreo directly from our website. Insert your new Profile and fill in the rest of the required inputs.

14 Click Generate Code to test the Choreo and you'll see the value "true" for the Success output in the Output section. Assuming you emailed yourself, you can go to your inbox and see the email appear!

Create Your Android Program

15To send an email from your Android code, add the required Temboo imports. Then, copy the generated Code from your browser directly into your Android AsyncTask class's doInBackground method. Finally, add a return statement that prints out whether or not the Choreo ran successfully. Your code should look something like this:

import com.temboo.core.TembooSession;
import com.temboo.Library.Google.Gmail.SendEmail;
import com.temboo.Library.Google.Gmail.SendEmail.SendEmailInputSet;
import com.temboo.Library.Google.Gmail.SendEmail.SendEmailResultSet;

public class GmailTask extends AsyncTask<Void, Void, String> {

    private TextView textView;

    public GmailTask(TextView textView){
        this.textView = textView;
    }

    @Override
    protected String doInBackground(Void... arg0) {
        try {
            // Create a new Temboo session.
            TembooSession session = new TembooSession("ACCOUNT_NAME", "APP_NAME", "APP_KEY");

            // Instantiate the Google.Gmail.SendEmail Choreo, using the session object.
            SendEmail sendEmailChoreo = new SendEmail(session);

            // Get an InputSet object for Google.Gmail.SendEmail Choreo.
            SendEmailInputSet sendEmailInputs = sendEmailChoreo.newInputSet();

            // Set Profile to use for execution. Note this value should be the name of the Profile that you created earlier. 
            sendEmailInputs.setCredential("YOUR_GMAIL_PROFILE_NAME");

            // Set inputs for Google.Gmail.SendEmail Choreo.
            sendEmailInputs.set_MessageBody("Hello world!");
            sendEmailInputs.set_Subject("Test from Temboo");
            sendEmailInputs.set_FromAddress("martha-temboo@gmail.com");
            sendEmailInputs.set_ToAddress("martha-temboo@gmail.com");

            // Execute Google.Gmail.SendEmail Choreo
            SendEmailResultSet sendEmailResults = sendEmailChoreo.execute(sendEmailInputs);

            return "SendEmail succeeded? " + sendEmailResults.get_Success();
        } catch(Exception e) {
            // if an exception occurred, log it
            Log.e(this.getClass().toString(), e.getMessage());
        }
        return null;
    }

    protected void onPostExecute(String result) {
        try {
            textView.setText(result);
        } catch(Exception e) {
            // if an exception occurred, show an error message
            Log.e(this.getClass().toString(), e.getMessage());
        }
    }
}

16Add an invocation of GmailTask to your main activity's onCreate method.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView textView = (TextView) findViewById(R.id.result);
    new GmailTask(textView).execute();
}

17Run your app and check your inbox - your email should be waiting.

What next?

We're all finished! Now your Android application is sending email via your Gmail account. Why not check out the rest of the 2000+ Choreos in our Library and think about how you could extend this example by combining it with something else.

Once you've got your code up and running, you're ready to move on and do more. From monitoring your running applications, to moving your generated Temboo code to your preferred development environment and sharing it with colleagues, collaborators and friends - we've got you covered.

Need help?

We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.


Back