0
votes

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.

1

1 Answers

1
votes

The Gmail API documentation says:

You can make upload requests in any of the following ways. Specify the method you are using with the uploadType request parameter.

Simple upload: uploadType=media. For quick transfer of smaller files, for example, 5 MB or less.

Multipart upload: uploadType=multipart. For quick transfer of smaller files and metadata; transfers the file along with metadata that describes it, all in a single request.

Resumable upload: uploadType=resumable. For reliable transfer, especially important with larger files. With this method, you use a session initiating request, which optionally can include metadata. This is a good strategy to use for most applications, since it also works for smaller files at the cost of one additional HTTP request per upload.

Since you want to send data > 5 MB, use the multipart or resumable option. Example for multipart: (taken from the docs as well):

public static MimeMessage createEmailWithAttachment(String to,
                                                        String from,
                                                        String subject,
                                                        String bodyText,
                                                        File file) throws MessagingException, IOException {
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        MimeMessage email = new MimeMessage(session);

        email.setFrom(new InternetAddress(from));
        email.addRecipient(javax.mail.Message.RecipientType.TO,
                new InternetAddress(to));
        email.setSubject(subject);

        MimeBodyPart mimeBodyPart = new MimeBodyPart();
        mimeBodyPart.setContent(bodyText, "text/plain");

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(mimeBodyPart);

        mimeBodyPart = new MimeBodyPart();
        DataSource source = new FileDataSource(file);

        mimeBodyPart.setDataHandler(new DataHandler(source));
        mimeBodyPart.setFileName(file.getName());

        multipart.addBodyPart(mimeBodyPart);
        email.setContent(multipart);

        return email;
    }