java 使用 android SDK 读取 Gmail 邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6096381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:18:57  来源:igfitidea点击:

Reading Gmail mails using android SDK

javaandroidsdkgmail

提问by Mohammad Moghimi

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?

我想在我自己的 android 应用程序中阅读 Gmail 邮件。有没有办法使用android sdk来做?如果没有,还有哪些其他选择?解析gmail atom?

采纳答案by Aracem

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

我在这里问和回答这个问题。您需要 Gmail.java 代码(在问题中有一个链接)并且您必须明白您不应该使用那个未记录的提供程序

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

有什么好的短代码示例可以简单地阅读新的 gmail 邮件吗?

回答by anhoppe

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

可以使用 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 herein order to get Step1+2 in the sample right.
  3. With the sample running you can use the information hereto 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;
    }
    
  1. 从官方示例开始启动GMailAPI,看这里
  2. 按照说明进行操作时,我发现在此处阅读有关应用程序签名的信息很有帮助,以便正确执行示例中的 Step1+2。
  3. 示例运行后,您可以使用此处的信息访问消息。您可以例如替换 MakeRequestTask.getDataFromApi 中的实现
  4. 请务必至少添加只读范围以获得适当的权限。在示例中,范围在数组中定义:

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

  5. 我的目的是阅读所有科目。我使用了以下代码(这是从官方示例中改编的 getDataFromApi 方法):

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