只要附件很小,一切似乎都有效
但是,当我尝试附加较大的文件(例如7MB)时,execute()
的{{1}}方法只会挂起。
我试图查看文档,如果我理解正确,我应该使用Send
API which actually performs upload
但是,我没有说明我应该在哪里提供这些参数。
这是电子邮件生成方法:
send
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;
}
答案 0 :(得分:2)
正如您所提到的,我认为您达到了允许邮件大小的上限。我不认为(有人请纠正我,如果我错了)Java Gmail API客户端对超过此大小的邮件有任何内置支持,因此由您来实现它。 / p>
在幕后,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}"
请注意网址中的upload
,请求正文中的uploadType=multipart
网址参数,message/rfc822
为Content-Type
和message non-encoded
。 This answer可能会给你一些启发。
所以你可能需要绕过(再次,有人纠正我,如果我错了)Java客户端并使用其他库来自己制作常规的http请求。