【问题标题】:Email with Multiple Attachments using Graph API (>4 MB)使用 Graph API 的带有多个附件的电子邮件 (>4 MB)
【发布时间】:2020-11-12 01:32:33
【问题描述】:

我正在使用 Microsoft-Graph API 1.4 版并尝试使用以下代码发送带有多个附件(大小大于 4MB)的邮件..

val address = EmailAddress()
address.address = "vivek@domain.com"
val recipient = Recipient()
recipient.emailAddress = address

val message = MyMessage()
message.subject = "Test E-Mail"
message.body = getItemBody()
message.toRecipients = Collections.singletonList(recipient)

val att = FileAttachment()
att.contentBytes = File("/home/user/file.pdf").readBytes()
att.contentType = "text/pdf"
att.name = "file.pdf"
att.oDataType = "#microsoft.graph.fileAttachment"

val att2 = FileAttachment()
att2.contentBytes = "hello there! from second file".toByteArray(StandardCharsets.UTF_8)
att2.contentType = "text/plain"
att2.name = "hi2.txt"
att2.oDataType = "#microsoft.graph.fileAttachment"

message.addAttachment(att)
message.addAttachment(att2)

graphClient.me()
.sendMail(message,false)
.buildRequest()
.post();

作为回应,我收到以下错误..

{
    "status": "INTERNAL_SERVER_ERROR",
    "exception": "GraphServiceException",
    "message": "Error code: BadRequest\nError message: The maximum request length supported is 4MB.\n\nPOST https://graph.microsoft.com/v1.0/me/microsoft.graph.sendMail\nSdkVersion : graph-java-v1.4.0\nAuthorization : Bearer eyJ0eXAiOiJKV1QiLCJub25jZSI[...]\n{\"message\":{\"attachments\":[{\"contentBytes\":\"JVBERi[...]\n\n413 : Request Entity Too Large\n[...]\n\n[Some information was truncated for brevity, enable debug logging for more details]",
    "existing": null,
    "errorData": null,
    "tenant": null
}

谁能帮我发送一封包含多个附件且大小大于 4MB 的邮件。

谢谢

【问题讨论】:

    标签: microsoft-graph-api microsoft-graph-mail


    【解决方案1】:

    要在总大小超过 4MB 时一次添加多个附件,您需要:

    1. 创建消息(作为草稿)
    2. 使用上传会话创建附件
    3. 使用您刚刚创建的会话上传附件(对每个附件重复)
    4. 发送草稿电子邮件

    这里有一个代码示例,它按照这些思路做了一些事情

        String draftSubject = "Draft Test Message " + Double.toString(Math.random()*1000);
        User me = graphClient.me().buildRequest().get();
        Recipient r = new Recipient();
        EmailAddress address = new EmailAddress();
        address.address = me.mail;
        r.emailAddress = address;
        Message message = new Message();
        message.subject = draftSubject;
        ArrayList<Recipient> recipients = new ArrayList<Recipient>();
        recipients.add(r);
        message.toRecipients = recipients;
        message.isDraft = true;
        
        //Save the message as a draft
        Message newMessage = graphClient.me().messages().buildRequest().post(message);
    
        File file = new File("src/test/resources/largefile10M.blob");
    
        AttachmentItem attachmentItem = new AttachmentItem();
        attachmentItem.attachmentType = AttachmentType.FILE;
        attachmentItem.name = file.getName();
        attachmentItem.size = file.length();
        attachmentItem.contentType = "application/octet-stream";
    
        InputStream fileStream = OutlookTests.class.getClassLoader().getResourceAsStream("largefile10M.blob");
    
        long streamSize = attachmentItem.size;
    
        UploadSession uploadSession = graphClient.me()
                                    .messages(newMessage.id)
                                    .attachments()
                                    .createUploadSession(attachmentItem)
                                    .buildRequest()
                                    .post();
    
        ChunkedUploadProvider<AttachmentItem> chunkedUploadProvider = new ChunkedUploadProvider<>(uploadSession, testBase.graphClient, fileStream,
                streamSize, AttachmentItem.class);
        
        // Do the upload
        chunkedUploadProvider.upload(callback);
    
        //Send the drafted message
        graphClient.me().mailFolders("Drafts").messages(newMessage.id).send().buildRequest().post();
    

    这是回调的示例实现。

        IProgressCallback<AttachmentItem> callback = new IProgressCallback<AttachmentItem> () {
            @Override
            public void progress(final long current, final long max) {
                //Check progress
            }
            @Override
            public void success(final AttachmentItem result) {
                //Handle the successful response
            }
            
            @Override
            public void failure(final ClientException ex) {
                //Handle the failed upload
            }
        };
    

    【讨论】:

    • 我已将图形版本从 1.4 升级到 2.3 以执行您给定的示例。现在能否请您告诉我 chunkedUploadProvider.upload(callback) 中的回调可能是什么?
    • 感谢您的更新。现在我已经尝试了您提供的完全相同的代码。刚刚用我的 blob 文件更新了文件路径。邮件发送和接收成功,但没有附件。我们有什么遗漏的吗?
    • 我已经打印出了失败函数中的异常日志。它看起来像以下...... com.microsoft.graph.core.ClientException:上传会话失败。在 com.microsoft.graph.requests.extensions.ChunkedUploadRequest.upload(ChunkedUploadRequest.java:115) 在 com.microsoft.graph.concurrency.ChunkedUploadProvider.upload(ChunkedUploadProvider.java:186) 在 com.microsoft.graph.concurrency.ChunkedUploadProvider .upload(ChunkedUploadProvider.java:214)
    • 当前存在导致 SDK 失败的服务行为。我们已经实施了修复,它将与下一个版本 2.3.1 一起发布github.com/microsoftgraph/msgraph-sdk-java/issues/529
    • 我使用的是最近发布的 2.3.1 版本。但问题还是一样。仔细检查版本升级。我已刷新依赖项并验证应用程序是否使用 2.3.1 版本。它使用相同的 2.3.1 版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 2020-09-12
    相关资源
    最近更新 更多