2
votes

Trying to insert message loaded from MimeMessage (as shown in gmail api example), some messages are passing fine, but some throws this:

    com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
<HTML>
<HEAD>
<TITLE>Bad Request</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Bad Request</H1>
<H2>Error 400</H2>
</BODY>
</HTML>

    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:312)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1049)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)

The code is next:

     final ByteArrayOutputStream baos = new ByteArrayOutputStream();
     mime.writeTo(baos);
     String encodedEmail = Base64.encodeBase64URLSafeString(baos.toByteArray());
     tmpMail = new Message();
     tmpMail.setRaw(encodedEmail);
     client.users().messages().insert(this.taskInfo.getTargetAccount(), tmpMail).execute();

Without any other information, what could be a possible reason for that error ?

Thanks.

UPD. Looks like messages larger than 10Mb are giving this error. Google guys, can you advise ? Thanks

2
Are you sending this from a server? - abalos
Also, will you please post the code that sends this error? - abalos
@abalos I'm sending this from laptop , please see code in the edited post. Thanks - SlavaG

2 Answers

1
votes

Looks like what I have:-) A 35 MB limit is clearly documented here (https://developers.google.com/gmail/api/v1/reference/users/messages/insert, 4th line), not a problem for most because their existing e-mail systems limit email size to a somewhat smaller number.

final ByteArrayOutputStream output = new ByteArrayOutputStream();
message.writeTo(output);
final String raw = Base64.encodeBase64URLSafeString(output.toByteArray());
if (raw.length() > 35000000) {
    System.err.println("Message too large, skipping");
} else {
    try {
           getGmailService()
                        .users()
                        .messages()
                        .insert("me",
                                new Message().setRaw(raw)).execute();
0
votes

You need to use the media "/upload" method to send something that large. https://developers.google.com/gmail/api/guides/uploads