Home » Android » java – Reading Gmail mails using android SDK

java – Reading Gmail mails using android SDK

Posted by: admin May 14, 2020 Leave a comment


I want to read Gmail mails in my own android app. Is there anyway to do it using android sdk? If not, what are the other options? parsing gmail atom?

How to&Answers:

I ask and answer that question here.
You need Gmail.java code (in the question there are a link) and you must understand that you shouldn’t use that undocumented provider

Are there any good short code examples that simply read a new gmail message?


It’s possible using the GMail API, here are some steps I found helpful.

  1. Start with the official sample to get the GMailAPI started, see here
  2. When following the instructions I found it helpful to read about the app signing here in order to get Step1+2 in the sample right.
  3. With the sample running you can use the information here to access messages. You can e.g. replace the implementation in MakeRequestTask.getDataFromApi
  4. Be sure to add at least the read-only scope for proper permissions. In the sample the scopes are defined in an array:

    private static final String[] SCOPES = { GmailScopes.GMAIL_LABELS, mailScopes.GMAIL_READONLY };

  5. My intention was to read all subjects. I used the following code (which is the adapted getDataFromApi method from the official sample):

     private List<String> getDataFromApi() throws IOException {
         // Get the labels in the user's account. "me" referes to the authentized user.
         String user = "me";
         List<String> labels = new ArrayList<String>();
         ListMessagesResponse response = mService.users().messages().list(user).execute();
         for (Message message : response.getMessages()) {
             Message readableMessage = mService.users().messages().get(user, message.getId()).execute();
             if (readableMessage.getPayload() != null) {
                 for (MessagePartHeader header : readableMessage.getPayload().getHeaders()) {
                     if (header.getName().compareToIgnoreCase("Subject") == 0) {
        return labels;