如何在 Gmail 中获取完整的邮件正文?

2022-01-23 00:00:00 gmail-api gmail java

我想获得完整的消息正文.所以我尝试:

I want to get full message body. So I try:

Message gmailMessage = service.users().messages().get("me", messageId).setFormat("full").execute();

为了得到身体,我尝试:

That to get body, I try:

gmailMessage.getPayload().getBody().getData()

但结果总是null.如何获取完整的邮件正文?

but result always null. How to get full message body?

推荐答案

要从您的 gmailMessage 中获取数据,您可以使用 gmailMessage.payload.parts[0].body.data.如果要将其解码为可读文本,可以执行以下操作:

To get the data from your gmailMessage, you can use gmailMessage.payload.parts[0].body.data. If you want to decode it into readable text, you can do the following:

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;

System.out.println(StringUtils.newStringUtf8(Base64.decodeBase64(gmailMessage.payload.parts[0].body.data)));

相关文章