How to get the full text of a message in Gmail?

I want to get the full text of the message. Therefore, I try:

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

To get the body, I try:

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

but the result is always null . How to get the full text of the message?

+6
source share
6 answers

I tried this way since message.getPayload (). getBody (). getParts () has always been null

 import com.google.api.client.repackaged.org.apache.commons.codec.binary.Base64; import com.google.api.client.repackaged.org.apache.commons.codec.binary.StringUtils; 

(...)

 Message message = service.users().messages().get(user, m.getId()).execute(); MessagePart part = message.getPayload(); System.out.println(StringUtils.newStringUtf8(Base64.decodeBase64(part.getBody().getData()))); 

And the result is a clean HTML string

+5
source

I found a more interesting way to solve the complete message of the body (and not just the body):

 System.out.println(StringUtils.newStringUtf8( Base64.decodeBase64 (message.getRaw()))); 
+4
source

To get 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))); 
+3
source

If you have a message (com.google.api.services.gmail.model.Message), you can use the following methods:

 public String getContent(Message message) { StringBuilder stringBuilder = new StringBuilder(); try { getPlainTextFromMessageParts(message.getPayload().getParts(), stringBuilder); byte[] bodyBytes = Base64.decodeBase64(stringBuilder.toString()); String text = new String(bodyBytes, "UTF-8"); return text; } catch (UnsupportedEncodingException e) { logger.error("UnsupportedEncoding: " + e.toString()); return message.getSnippet(); } } private void getPlainTextFromMessageParts(List<MessagePart> messageParts, StringBuilder stringBuilder) { for (MessagePart messagePart : messageParts) { if (messagePart.getMimeType().equals("text/plain")) { stringBuilder.append(messagePart.getBody().getData()); } if (messagePart.getParts() != null) { getPlainTextFromMessageParts(messagePart.getParts(), stringBuilder); } } } 

It combines all parts of the messages with mimeType "text / plain" and returns them as a single line.

+1
source

Here is the solution in C # code gmail API v1 to read the contents of the email body:

  var request = _gmailService.Users.Messages.Get("me", mail.Id); request.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Full; 

and solve the data error

  var res = message.Payload.Body.Data.Replace("-", "+").Replace("_", "/"); byte[] bodyBytes = Convert.FromBase64String(res); string val = Encoding.UTF8.GetString(bodyBytes); 
0
source

@Tholle comment base I did something like this

 Message message = service.users().messages() .get(user, messageHolder.getId()).execute(); System.out.println(StringUtils.newStringUtf8(Base64.decodeBase64( message.getPayload().getParts().get(0).getBody().getData()))); 
0
source

Source: https://habr.com/ru/post/981218/


All Articles