使用 Graph API 的带有多个附件的电子邮件 (> 4 MB) [英] Email with Multiple Attachments using Graph API (>4 MB)

查看:118
本文介绍了使用 Graph API 的带有多个附件的电子邮件 (> 4 MB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Microsoft-Graph API version 1.4 and trying to send mail with multiple attachments (with size greater then 4MB) using following code..

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();

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

In response, I'm getting following error..

{
    "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 的邮件.

Can anyone help me to send a mail with multiple attachment with size greater then 4MB.

谢谢

推荐答案

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

In order to add multiple attachments at once when their total size exceeds 4MB, you need:

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

这是一个代码示例,可以按照这些方式执行某些操作

Here is a code sample that does something along those lines

    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
        }
    };

这篇关于使用 Graph API 的带有多个附件的电子邮件 (> 4 MB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆