com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146) 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:321) at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469) at org.gradle.GradleTest.insertMessage(GradleTest.java:132) at org.gradle.GradleTest.main(GradleTest.java:173)
This exception occurs only when we try to insert mail having large attachments(more than 5mb).
If attachment is smaller then mail is getting inserted properly.
To insert mail I tried :
File file = new File(email);
FileInputStream fis = new FileInputStream(file);
long fileSize = file.length();
byte[] buf = new byte[(int)fileSize];
int readNum = fis.read(buf);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(buf);
String encodedEmail = Base64.encodeBase64URLSafeString(baos.toByteArray());
Message message = new Message();
message.setRaw(encodedEmail);
message = service.users().messages().insert(userId, message).execute();
Please suggest solution to insert mails having attachment more than 5mb.