Azad KAYA
Azad KAYA

Reputation: 29

I want to read Gmail messages in Android

I want to get user email messages or tokens from Gmail in an Android application. How can I get a user's messages and token from Gmail? I need an example application.

Upvotes: 1

Views: 2252

Answers (2)

OriEng
OriEng

Reputation: 1573

You can use the example from here:

https://developers.google.com/gmail/api/quickstart/android

Later, instead request the labels you need to ask for messages so you need to update function 'getDataFromApi' to get messages :

 private static final long MAX_RESULTS_PER_REQUEST = 20;
private void getDataFromApi() throws IOException {
List<String> labelsId = new ArrayList<>();
labelsId.add("INBOX");
ListMessagesResponse response=null;
response = mService.users().messages().list("me").setMaxResults(MAX_RESULTS_PER_REQUEST).setLabelIds(labelsId).execute();
List<Message> messages = response.getMessages();
for (Message message : messages) {
     Message curMessage = mService.users().messages().get("me", message.getId()).execute();
     System.out.println("cur message==>"+curMessage);    
   }
}

Upvotes: 2

ziLk
ziLk

Reputation: 3200

You can get this using Content Provider API of Gmail

The Android Gmail app starting in versions 2.3.6 (Froyo/Gingerbread) and 4.0.5 (Honeycomb/ICS) includes a new Content Provider API that third party developers can use to retrieve label information like name and unread count, and stay updated as that information changes

To see an example of this API in action, check out the sample app.

Upvotes: 0

Related Questions