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

Sag*_*Low 9 email android gmail-api

我尝试了以下代码来创建包含大附件的多部分电子邮件:

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();
Run Code Online (Sandbox Code Playgroud)

这与本指南中提供的内容非常相似,但是,当我尝试添加大于〜5mb的文件时,执行函数会挂起并且不会返回(我希望错误或至少超时但是这样是另一个问题)

经过一些搜索,我发现我需要以某种方式做upload请求(见这里),Gmail API看起来正确的下面的API :

Send send(java.lang.String userId, com.google.api.services.gmail.model.Message content, com.google.api.client.http.AbstractInputStreamContent mediaContent)
Run Code Online (Sandbox Code Playgroud)

很遗憾,我找不到任何有关其用法的文档或说明.
当我试图把附件原始,因为mediaContent我得到一个错误,说明唯一支持的mime类型message/rfc822,所以我尝试MimeBodyPartfor上面的循环中创建并使用它,但看起来附件被忽略了.

如何使用Gmail client API和"上传"附件?

Fra*_* C. 5

我知道这个问题已经有几个月了,但在遇到同样的问题后,我能够弄清楚。

我的实现与您的非常相似,我更改的只是最后两行。

所以不要使用:

Message message = createMessageWithEmail(mimeMessage);

service.users().messages().send(from, message).execute();
Run Code Online (Sandbox Code Playgroud)

用:

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
mimeMessage.writeTo(bytes);

ByteArrayContent content = new ByteArrayContent("message/rfc822", bytes.toByteArray());
service.users().messages().send(from, null, content).execute();
Run Code Online (Sandbox Code Playgroud)