开发者

Reading Gmail mails using android SDK

I want to read Gmail mails in my own and开发者_高级运维roid app. Is there anyway to do it using android sdk? If not, what are the other options? parsing gmail atom?


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) {
                         labels.add(header.getValue());
                    }
                }
            }
        }
    
        return labels;
    }
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜