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
Reading Gmail mails using android SDK
提问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?
回答by anhoppe
It's possible using the GMail API, here are some steps I found helpful.
可以使用 GMail API,这里有一些我觉得很有帮助的步骤。
- Start with the official sample to get the GMailAPI started, see here
- When following the instructions I found it helpful to read about the app signing herein order to get Step1+2 in the sample right.
- With the sample running you can use the information hereto access messages. You can e.g. replace the implementation in MakeRequestTask.getDataFromApi
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 };
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; }
- 从官方示例开始启动GMailAPI,看这里
- 按照说明进行操作时,我发现在此处阅读有关应用程序签名的信息很有帮助,以便正确执行示例中的 Step1+2。
- 示例运行后,您可以使用此处的信息访问消息。您可以例如替换 MakeRequestTask.getDataFromApi 中的实现
请务必至少添加只读范围以获得适当的权限。在示例中,范围在数组中定义:
private static final String[] SCOPES = { GmailScopes.GMAIL_LABELS, mailScopes.GMAIL_READONLY };
我的目的是阅读所有科目。我使用了以下代码(这是从官方示例中改编的 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; }