How to get full message body in Gmail?

somebody picture somebody · Jan 19, 2015 · Viewed 22.2k times · Source

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()

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

Answer

Tholle picture Tholle · Jan 20, 2015

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