简体   繁体   English

如何使用Android上的gmail客户端API发送带有大附件的电子邮件

[英]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 . 要实现它,您需要使用MediaHttpUploaderMediaUttpProgressListener

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.

相关问题 如何使用Gmail帐户从我的应用程序发送电子邮件? - How to send emails from my app using a Gmail account? 使用 Android GoogleSignIn 和 GmailScopes.GMAIL_SEND(gmail api) - Using Android GoogleSignIn with GmailScopes.GMAIL_SEND (gmail api) Gmail如何处理Android中的附件 - How does Gmail handle attachments in Android 在Android中使用Gmail API和OAuth2检查来自Gmail的新电子邮件 - Check for new emails from Gmail with Gmail API and OAuth2 in Android 如何从Android通过Gmail客户端发送自动电子邮件 - How to send automatic email via Gmail client from Android 如何使用SendGrid Api在单个电子邮件中发送多个附件? - How to send multiple attachments in single email using SendGrid Api? 无法使用意图从我的应用发送带有附件的电子邮件(Gmail) - Unable to send email with attachments from my app using intents (Gmail) 使用 gmail api 和 android 客户端获取完整的电子邮件正文内容 - Get the full email body content using gmail api and android client 如何在Android中使用Javamail API从应用程序同时从Gmail和Ymail发送邮件 - How to send mail from both Gmail and Ymail from an application using javamail api in android Android电子邮件客户端应用程序跟踪未读的Gmail电子邮件 - Android email client app tracking unread gmail emails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM