使用 Android SDK 阅读 Gmail 邮件

11

我想在我的安卓应用程序中阅读Gmail邮件。有没有使用Android SDK可以做到这一点的方法?如果没有,还有哪些其他选项?解析Gmail原子文件?


5
这里不接受与开发相关的问题。 - Matthew Read
2个回答

5

1
使用Gmail API是可能的,以下是我发现有用的一些步骤。
  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;
    }
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接