简体   繁体   中英

Error Writing Request Body to Server with Gmail API

I have started messing with the Gmail API for a side project, and was messing around with sending an email with an attachment. The attachment I've been trying to send is a large one, but it's only a 19.5MB audio file, and the documentation only says that the limit is 35MB. I've sent a different audio file that was less than that and it was able to be sent I get the following error every time I try to send the larger audio file:

        java.io.IOException: Error writing request body to server
           at java.base/sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.checkError(HttpURLConnection.java:3648)
           at java.base/sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.write(HttpURLConnection.java:3631)
           at java.base/java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:81)
           at java.base/java.io.BufferedOutputStream.write(BufferedOutputStream.java:127)
           at java.base/java.util.zip.DeflaterOutputStream.deflate(DeflaterOutputStream.java:253)
           at java.base/java.util.zip.DeflaterOutputStream.write(DeflaterOutputStream.java:211)
           at java.base/java.util.zip.GZIPOutputStream.write(GZIPOutputStream.java:146)
           at com.fasterxml.jackson.core.json.UTF8JsonGenerator._flushBuffer(UTF8JsonGenerator.java:1819)
           at com.fasterxml.jackson.core.json.UTF8JsonGenerator._writeStringSegments(UTF8JsonGenerator.java:1142)
           at com.fasterxml.jackson.core.json.UTF8JsonGenerator._writeLongString(UTF8JsonGenerator.java:456)
           at com.fasterxml.jackson.core.json.UTF8JsonGenerator.writeString(UTF8JsonGenerator.java:425)
           at com.google.api.client.json.jackson2.JacksonGenerator.writeString(JacksonGenerator.java:128)
           at com.google.api.client.json.JsonGenerator.serialize(JsonGenerator.java:117)
           at com.google.api.client.json.JsonGenerator.serialize(JsonGenerator.java:172)
           at com.google.api.client.json.JsonGenerator.serialize(JsonGenerator.java:106)
           at com.google.api.client.http.json.JsonHttpContent.writeTo(JsonHttpContent.java:78)
           at com.google.api.client.http.GZipEncoding.encode(GZipEncoding.java:49)
           at com.google.api.client.http.HttpEncodingStreamingContent.writeTo(HttpEncodingStreamingContent.java:51)
           at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:80)
           at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:981)
           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 EmailAPI.sendMessage(EmailAPI.java:179)
           at EmailSender.sendEmailWithAttachment(EmailSender.java:41)
           at EmailSender.sendEmails(EmailSender.java:67)
           at EmailSender.main(EmailSender.java:19)

Everything that I find doesn't seem to help. I have tried:

          //This is the way that it is in the quickstart project
          mimeBodyPart = new MimeBodyPart();
          DataSource source = new FileDataSource(file);
          mimeBodyPart.setDataHandler(new DataHandler(source));
          mimeBodyPart.setFileName(file.getName());

          //I have tried putting both of these inplace of the above code but nothing worked
          mimeBodyPart.setContent(file, "audio/MPEG");
          mimeBodyPart.attachFile(file);

The only thing that I have to go on is that the server might be cutting off the connection while sending the request, but I don't know if that is the issue or if it's something else.

To send emails larger than 5MB you must use either multipart upload or resumable upload (see docs for details).

In Java Gmail API client library, 2-argument version of send method sends a simple upload request, which can upload up to 5MB. To send a resumable upload request, use 3-argument version of send method. Using resumable upload you can send up to 35MB.

Here is a method that sends javax.mail.internet.MimeMessage using resumable upload :

import com.google.api.client.http.ByteArrayContent;
import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.model.Message;

import javax.mail.internet.MimeMessage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import javax.mail.MessagingException;
private Message sendMessage(Gmail gmail, String userId, MimeMessage mimeMessage, List<String> labelIds, String threadId) throws IOException, MessagingException {
    // Create Message instance containing email message metadata
    Message metadata = new Message()
            .setLabelIds(labelIds)
            .setThreadId(threadId);

    // Create byte array containing email message data (in RFC 2822 format)
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    mimeMessage.writeTo(baos);
    ByteArrayContent rawMessageBytes = new ByteArrayContent("message/rfc822", baos.toByteArray());

    return gmail.users().messages()
            .send(userId, metadata, rawMessageBytes)
            .execute();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM