stellenberger
stellenberger

Reputation: 145

Encoding Gmail API Message class into Raw format

I am using the GMail API in Java and have an issue.

I have the following code:

Message message = new Message();

m.setPayload(*set the message part*)
// setter code for Message ommited for brevity

service.users().messages().send("me", m).execute()

This gives me back an error message saying:

Required parameter Message.getRaw() must be specified

My question is; how can I take the Message instance from Gmail and encode it in a base64, URLStringSafe format?

Upvotes: 1

Views: 488

Answers (1)

Lunatic
Lunatic

Reputation: 1906

You asked

How can I take the Message instance from Gmail and encode it in a base64, URLStringSafe format

As the place there is no specification of data type of your message payload let imagine we have MimeMessage instance, use ByteArrayOutputStream and Base64.encodeBase64URLSafeString() to extract URLStringSafe format.

public Message publishMessage(MimeMessage mime){
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    mime.writeTo(buffer);
    byte[] bytes = buffer.toByteArray();
    Message message = new Message();
    message.setRaw(Base64.encodeBase64URLSafeString(bytes));

    //some optional operation on your message perhaps
    ...

    return message;
}

Upvotes: 1

Related Questions