0

Email Not been recieved with attachments when I try to use Uploadsession using Graph API. can someone help me uderstand why this is happening. I have not recieved any error.

      Message draft = await graphServiceClient.Users["UserID"].Messages.Request().AddAsync(email);
        //Message draft = graphServiceClient.Users["UserID"].Mailfolders.Drafts.Messages.Request().AddAsync(email);

        var stream = System.IO.File.Open(@"C:\attach\DYN28_6579332_33242556.csv", System.IO.FileMode.Open, FileAccess.Read, FileShare.None);
        var attachmentItem = new AttachmentItem
        {
            AttachmentType = AttachmentType.File,
            Name = "DYN28_6579332_33242556.csv",
            Size = stream.Length
        };
        var uploadSession = await graphServiceClient.Users["Userid"].Messages[draft.Id]
                                .Attachments
                                .CreateUploadSession(attachmentItem)
                                .Request()
                                .PostAsync();
        var maxSlicesize = 320 * 1024;
        var largeFileUploadTask = new LargeFileUploadTask<FileAttachment>(uploadSession, stream, maxSlicesize);
        IProgress<long> progress = new Progress<long>(prog => {
            Console.WriteLine($"Uploaded {prog} bytes of {stream.Length} bytes");
        });

            // Upload the file
            var uploadResult = await largeFileUploadTask.UploadAsync(progress);

            if (uploadResult.UploadSucceeded)
            {
            // The ItemResponse object in the result represents the
            // created item.
            //Console.WriteLine($"Upload complete, item ID: {uploadResult.ItemResponse.Id}");
            Console.WriteLine("upload completed");
            }

  Finally sending email with 
                                                                      
                                                                         
  await graphServiceClient.Users["userid"].Messages[draft.Id]
                                  .Send()
                                  .Request()
                                  .PostAsync();
User123
  • 17
  • 6

1 Answers1

0

There is a limit of 4MB on a single request in the Graph API. To send larger attachments, you need to first create an upload session against the email message/calendar event, and upload your attachment in a number of requests as part of this session. AFAIK each of the smaller POST requests would also need to stay below the 4MB limit.

You can find more detailed documentation and a sample walkthrough here.

POST https://graph.microsoft.com/v1.0/me/messages/AAMkADI5MAAIT3drCAAA=/attachments/createUploadSession
Content-type: application/json

{
  "AttachmentItem": {
    "attachmentType": "file",
    "name": "flower",
    "size": 3483322
  }
}
Neel Shah
  • 81
  • 1
  • 7
  • Smaller attachements should be less than 3MB? In our scenario we have customer data where sometimes the attachments is more than 15MB..In this case it will fail along.Do we have any other option where I can send all the attachments in single mail.Or any version needs to be updated – User123 Mar 04 '21 at 07:59
  • An upload session is used to upload a file larger than 4MB to the server in parts. The attachment is yet sent as a single item within a single email message. It is only uploaded in parts to the server. The recipient will see it as a single file/item. – Neel Shah Mar 05 '21 at 05:28
  • In case you want to attach multiple large files to an email message, you can create separate upload sessions for each of them. A single upload session is for uploading a large file in pieces to the server. You can create multiple sessions simultaneously. After you're done uploading the attachments, you can send the email. – Neel Shah Mar 05 '21 at 05:32
  • I have tried with console application to send large attachments of size 16MB with email.I am able to get the size and upload session created, drafted the email but i have not recieved any email. – User123 Mar 05 '21 at 13:06
  • Can you please share a screenshot of the error you are facing? – Neel Shah Mar 07 '21 at 05:21
  • I am not getting any error. but the delivery failed to below recipients. And i have one more question as i have multiple attachments with different sizes to send through mail.How can I Achieve this? – User123 Mar 08 '21 at 06:27
  • There are two things here - uploading attachments and then sending the message. You first create a message, then upload all your attachments on this draft message. When you are done uploading all these, then send the email. So the API calls you make are: create message, create upload session, upload attachment parts, send message. You can repeat the attachment calls for all your attachments. – Neel Shah Mar 11 '21 at 15:12
  • This same list of steps is also mentioned in this related answer [here](https://stackoverflow.com/questions/64675294/send-mail-with-mixed-sized-attachments-4mb-using-graph-api-version-2-3-2?rq=1) – Neel Shah Mar 11 '21 at 15:14