使用java中的com.google.api.service.gmail.model.Message对象读取gmail消息正文?

时间:2016-09-19 16:32:24

标签: java google-api mime gmail-api google-api-client

我尝试使用GmailAPI阅读gmail邮件。我成功地获得了消息对象。

我得到了对象com.google.api.services.gmail.model.Message及其.toString() {"id":"157433a7e******","threadId":"157433a7e******"}

但是我无法读取对象的内容message.getPayload()返回null,.getSnippet()返回null,.getRaw()也返回null

对象API:https://developers.google.com/resources/api-libraries/documentation/gmail/v1/java/latest/com/google/api/services/gmail/model/Message.html

我已尝试过

中提到的其他方法

How to get full message body in Gmail?

我想知道mail subject, dates, sender, label, body

有人可以给我任何指针吗?

这是我使用

的项目来源

com.google.api-client %% google-api-client %% 1.22.0

com.google.oauth-client %% google-oauth-client-jetty %% 1.22.0

com.google.apis %% google-api-services-gmail %% v1-rev48-1.22.0

提前致谢

1 个答案:

答案 0 :(得分:3)

如果您尝试API Explorer for listing messages,您会看到回复中只包含id条消息和threadId。您必须向get the content of the message提出额外请求。

Message test = service.users().messages().get("me", "157433a7e******").setFormat("full").execute();

然后你只需要从消息和traverse the parts to get the content of the message获取字段。

相关问题