[英]How to send emails with large attachments using gmail client API on Android
I've tried the following code to create multipart email with large attachments : 我尝试了以下代码来创建包含大附件的多部分电子邮件:
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeBodyPart mimeBodyText = new MimeBodyPart();
mimeBodyText.setHeader("Content-Type", "text/html; charset=\"UTF-8\"");
mimeBodyText.setContent(body, "text/html");
Multipart mp = new MimeMultipart();
mp.addBodyPart(mimeBodyText);
if (attachments != null && attachments.size() > 0) {
for (Uri uri : attachments) {
MimeBodyPart mimeBodyAttachment = new MimeBodyPart();
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
{
mimeBodyAttachment.setFileName(fileName);
mimeBodyAttachment.setHeader("Content-Type", mimeType + "; name=\"" + fileName + "\"");
DataSource source = new ByteArrayDataSource(is, mimeType);
mimeBodyAttachment.setDataHandler(new DataHandler(source));
mimeBodyAttachment.setHeader("Content-Transfer-Encoding", "base64");
mimeBodyAttachment.setDisposition(MimeBodyPart.ATTACHMENT);
mp.addBodyPart(mimeBodyAttachment);
} catch (IOException e) {
throw new MessagingException(e.getMessage());
}
}
}
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(from));
mimeMessage.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(recipient));
mimeMessage.setSubject(subject);
mimeMessage.setContent(mp);
Message message = createMessageWithEmail(mimeMessage);
service.users().messages().send(from, message).execute();
Which is pretty similar to what is presented in this guide , however, when I try to add a file which is larger than ~5mb, the execute function hangs and doesn't return (I would expect an error or at least a timeout but this is another issue) 这与本指南中的内容非常相似,但是,当我尝试添加大于〜5mb的文件时,执行函数会挂起并且不会返回(我希望错误或至少超时但是这样是另一个问题)
After some search, I've found I need to somehow do upload
request ( see here ), the following API in the Gmail API
looks right : 经过一些搜索后,我发现我需要以某种方式执行
upload
请求( 请参阅此处 ), Gmail API
的以下Gmail API
看起来正确:
Send send(java.lang.String userId, com.google.api.services.gmail.model.Message content, com.google.api.client.http.AbstractInputStreamContent mediaContent)
Unfortunately I couldn't find any doc or instruction about its usage. 很遗憾,我找不到任何有关其用法的文档或说明。
When I tried to put the attachment raw as mediaContent
I got an error saying the only supported mime type is message/rfc822
, so I've tried taking the MimeBodyPart
I'm creating in the for
loop above and use it, but it looks like the attachment is just ignored. 当我试图将附件原始作为
mediaContent
我得到一个错误,说唯一支持的mime类型是message/rfc822
,所以我尝试使用我在上面的for
循环中创建的MimeBodyPart
并使用它,但它看起来像附件被忽略了。
How should one use the Gmail client API
and 'upload' attachments ? 如何使用
Gmail client API
和“上传”附件?
I know this question is a couple of months old, but after running into the same problem, I was able to figure it out. 我知道这个问题已经有几个月了,但在遇到同样的问题后,我能够弄清楚。
My implementation is very similar to yours, all I changed was the last two lines. 我的实现与你的实现非常相似,我改变的是最后两行。
So instead of using: 所以不要使用:
Message message = createMessageWithEmail(mimeMessage);
service.users().messages().send(from, message).execute();
Use: 使用:
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
mimeMessage.writeTo(bytes);
ByteArrayContent content = new ByteArrayContent("message/rfc822", bytes.toByteArray());
service.users().messages().send(from, null, content).execute();
The Media Upload/Download section of the Java client for Google API indicates the steps to do this. 用于Google API的Java客户端的“ 媒体上载/下载”部分指出了执行此操作的步骤。
Its indicated that you'll need to use resumable media upload
as the upload option for this scenario. 它表明您需要使用
resumable media upload
作为此方案的上传选项。 To implement it, you'll need to use MediaHttpUploader
and MediaUttpProgressListener
. 要实现它,您需要使用
MediaHttpUploader
和MediaUttpProgressListener
。
The referenced page also has an example that can work on GMail if you pass the InputStreamContent
in the 3rd parameter of send()
. 如果您在
send()
的第3个参数中传递InputStreamContent
,则引用的页面还有一个可以在GMail上工作的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.