gpt4 book ai didi

android - 在 Android 中使用 Gmail API 发送带附件的电子邮件(execute() 挂起)

转载 作者:行者123 更新时间:2023-11-29 00:04:13 24 4
gpt4 key购买 nike

只要附件很小,一切似乎都能正常工作。
但是,当我尝试附加更大的文件(例如 7MB)时,Sendexecute() 方法挂起。
我试图查看文档,如果我理解正确,我应该使用 send API which actually performs upload但是,我没有想好应该在哪里提供这些参数。
这是电子邮件生成方法:

public MimeMessage toMimeMessage(String from, Context context) throws MessagingException {
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);

MimeMessage mimeMessage = new MimeMessage(session);

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

MimeBodyPart mimeBodyText = new MimeBodyPart();
mimeBodyText.setContent(body, "text/html");
mimeBodyText.setHeader("Content-Type", "text/plain; charset=\"UTF-8\"");

Multipart mp = new MimeMultipart();
mp.addBodyPart(mimeBodyText);

if (attachments != null && attachments.size() > 0) {
MimeBodyPart mimeBodyAttachments = new MimeBodyPart();
for (Uri uri : attachments) {
String fileName = UriUtils.getFileName(uri, context);
String mimeType = UriUtils.getMimeType(uri, context);
Log.d(TAG, "Generating file info, uri=" + uri.getPath() + ", mimeType=" + mimeType);
FileInputStream is = UriUtils.generateFileInfo(context, uri, mimeType);
if (is == null) {
throw new MessagingException("Failed to get file for uri=" + uri.getPath());
}
try
{
DataSource source = new ByteArrayDataSource(is, mimeType);
mimeBodyAttachments.setDataHandler(new DataHandler(source));
mimeBodyAttachments.setFileName(fileName);
mimeBodyAttachments.setHeader("Content-Type", mimeType + "; name=\"" + fileName + "\"");
} catch (IOException e) {
throw new MessagingException(e.getMessage());
}
}
mimeBodyAttachments.setHeader("Content-Transfer-Encoding", "base64");
mimeBodyAttachments.setDisposition(MimeBodyPart.ATTACHMENT);
mp.addBodyPart(mimeBodyAttachments);
}

mimeMessage.setContent(mp);

return mimeMessage;
}

.

Message createMessageWithEmail(MimeMessage mimeMessage) throws MessagingException, IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
mimeMessage.writeTo(bytes);
String encodedEmail = Base64.encodeBase64URLSafeString(bytes.toByteArray());
Message message = new Message();
message.setRaw(encodedEmail);
return message;
}

其次是:

MimeMessage mimeMessage = email.toMimeMessage(userId, context);
Message message = createMessageWithEmail(mimeMessage);
Gmail.Users.Messages messages = service.users().messages();
Gmail.Users.Messages.Send send = messages.send(userId, message);
send.execute(); // method hangs when using large attachment

最佳答案

如您所述,我认为您达到了允许的消息大小上限。我不认为(如果我错了,请有人纠正我)Java Gmail API 客户端对超过这个大小的邮件有任何内置支持,所以由你来实现它。

在幕后,messages.send 方法产生一个常规的 http POST 请求:

POST https://www.googleapis.com/gmail/v1/users/{USER_ID}/messages/send
Content-Type: application/json
Authorization: Bearer {ACCESS_TOKEN}

{
"raw": "{MESSAGE_URL_SAFE_BASE64_ENCODED}"
}

正如您发现的那样,这仅适用于总大小约为 5 mb 的情况。如果您想使用 35 mb 的最大限制,您需要执行以下操作:

POST https://www.googleapis.com/upload/gmail/v1/users/{USER_ID}/messages/send?uploadType=multipart
Content-Type: message/rfc822
Authorization: Bearer {ACCESS_TOKEN}

"{MESSAGE_IN_RFC822_FORMAT}"

注意 URL 中的 uploaduploadType=multipart URL 参数,message/rfc822Content-Type未编码的消息 在请求正文中。 This answer可能会有所启发。

因此您可能需要绕过 Java 客户端(同样,如果我错了,请有人纠正我)并使用其他一些库自己发出常规的 http 请求。

关于android - 在 Android 中使用 Gmail API 发送带附件的电子邮件(execute() 挂起),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35225995/

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