[英]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.我已经开始在一个副项目中使用 Gmail API,并且正在发送带有附件的电子邮件。 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.
我一直试图发送的附件很大,但它只有 19.5MB 的音频文件,而且文档只说限制为 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).要发送大于 5MB 的电子邮件,您必须使用分段上传或可恢复上传(有关详细信息,请参阅文档)。
In Java Gmail API client library, 2-argument version of send
method sends a simple upload request, which can upload up to 5MB.在 Java Gmail API 客户端库中,
send
方法的 2 参数版本发送一个简单的上传请求,最多可以上传 5MB。 To send a resumable upload request, use 3-argument version of send
method.要发送可恢复的上传请求,请使用
send
方法的 3 参数版本。 Using resumable upload you can send up to 35MB.使用可恢复上传,您最多可以发送 35MB。
Here is a method that sends javax.mail.internet.MimeMessage
using resumable upload :这是使用可恢复上传发送
javax.mail.internet.MimeMessage
:
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.