gpt4 book ai didi

java - 将邮件插入 Gmail 时出现 400 错误请求,前提是邮件包含大附件(超过 5 mb)

转载 作者:行者123 更新时间:2023-12-01 09:35:13 25 4
gpt4 key购买 nike

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)

仅当我们尝试插入具有大附件(超过 5mb)的邮件时才会出现此异常。

如果附件较小,则邮件会正确插入。

插入邮件我尝试过:

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();

请建议插入附件超过 5mb 的邮件的解决方案。

最佳答案

Gmail API documentation说:

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. Forquick transfer of smaller files and metadata; transfers the file alongwith metadata that describes it, all in a single request.

Resumableupload: uploadType=resumable. For reliable transfer, especiallyimportant with larger files. With this method, you use a sessioninitiating request, which optionally can include metadata. This is agood strategy to use for most applications, since it also works forsmaller files at the cost of one additional HTTP request per upload.

由于您要发送大于 5 MB 的数据,请使用多部分或可恢复选项。多部分示例:(也取自文档):

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;
}

关于java - 将邮件插入 Gmail 时出现 400 错误请求,前提是邮件包含大附件(超过 5 mb),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39036703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com