Reading Gmail mails using android SDK

你。 提交于 2019-12-22 03:58:19

问题


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?


回答1:


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?




回答2:


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;
    }
    


来源:https://stackoverflow.com/questions/6096381/reading-gmail-mails-using-android-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!