使用与我所获得的API参考站点类似的代码,并在For循环开始时出现null异常错误。我已检查message
是否有附件,但它似乎是内联的。对于附件不是内联的其他消息,它确实有效。
如何获取内联附件?
public List<Attachment> getAttachments(String messageId) throws IOException
{
List<Attachment> attachments = new ArrayList<>();
Message message = SERVICE.users().messages().get(USER, messageId).execute();
List<MessagePart> parts = message.getPayload().getParts();
if (parts != null)
{
for (MessagePart part : parts)
{
if (part.getFilename() != null && part.getFilename().length() > 0)
{
String attId = part.getBody().getAttachmentId();
MessagePartBody attachPart = SERVICE.users().messages().attachments().
get(USER, messageId, attId).execute();
byte[] fileByteArray = Base64.decodeBase64(attachPart.getData());
Attachment attachment = new Attachment(part, attachPart, fileByteArray);
attachments.add(attachment);
}
}
}
return attachments;
}
答案 0 :(得分:2)
您可以通过message.getPayload().getBody()
我建议你先测试一下:
if(message.getPayload().getBody().size() > 0) {
// do your work with:
// message.getPayload().getBody()
} else {
// do your work with:
//message.getPayload().getParts()
}
有关详细信息,请参阅:
https://developers.google.com/gmail/api/v1/reference/users/messages和
尤其是这一行:"body": users.messages.attachments Resource
希望这有帮助。