Email with Multiple Attachments using Graph API (>4 MB)

前端 未结 1 976
遥遥无期
遥遥无期 2021-01-23 15:10

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 = Ema         


        
相关标签:
1条回答
  • 2021-01-23 15:50

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

    1. create the message (as a draft)
    2. create the attachment with an upload session
    3. upload the attachment using the session you just created (repeat for each attachment)
    4. send the draft email

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

    Here is a sample implementation for the callback.

        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
            }
        };
    
    0 讨论(0)
提交回复
热议问题