1

I am developing an aplication that send and email with one or multiple attachments via Microsoft Graph, but when try to upload file send me an error: ": Invalid total bytes specified in the Content-Range header" i asume that i must specifi Range Value in same where, but no idea.

This is my code:

private static async void SenMailUsingMicrosoftGraph(List<String>Destinations, List<String>Cc, string HidenCopy, string Body, string Title, List<FileInfo>Filess);
{
ClientSecretCredential credential = new ClientSecretCredential("MyTenantID", "MyClientId", "MyClientSecret");

        
        List<Recipient> recipientsDestinatarios = new List<Recipient>();
        List<Recipient> recipientsCopias = new List<Recipient>();

        foreach (var c in Destinations)
        {
            recipientsDestinatarios.Add(
                new Recipient
                {
                    EmailAddress = new EmailAddress
                    {
                        Address = c
                    }
                });
        }

        foreach (var mail in Cc)
        {

            recipientsCopias.Add(
                new Recipient
                {
                    EmailAddress = new EmailAddress
                    {
                        Address = mail
                    }
                });
        }
        #endregion

        var message = new Microsoft.Graph.Message
        {
            Subject = Title,
            Body = new ItemBody
            {
                ContentType = BodyType.Html,
                Content = Body
            },
            ToRecipients = recipientsDestinatarios
            ,
            CcRecipients = recipientsCopias
            ,
            BccRecipients = new List<Recipient>()
            {
                new Recipient
                {
                    EmailAddress=new EmailAddress{Address=Hiden}
                }
            }


        };

        GraphServiceClient graphClient = new GraphServiceClient(credential);
        #endregion

        #region adjuntar ficheros
        var msgResult = await graphClient.Users["myemail@mycompany.com"].MailFolders.Drafts.Messages
                                    .Request()
                                    .WithMaxRetry(9)
                                    .AddAsync(message);
        foreach (var Archivo in Filess)
        {
            var attachmentContentSize = Archivo.Length;
            var attachmentItem = new AttachmentItem
            {
                AttachmentType = AttachmentType.File,
                Name = Archivo.Name,
                Size = attachmentContentSize,
            };

            //initiate the upload session for large files 
            var uploadSession = await graphClient.Users["myemail@mycompany.com"].Messages[msgResult.Id].Attachments
                                                                    .CreateUploadSession(attachmentItem)
                                                                    .Request()
                                                                    .PostAsync();

            var maxChunkSize = 1024 * 320;
            var allBytes = System.IO.File.ReadAllBytes(Archivo.FullName);

            using (var stream = new MemoryStream(allBytes))
            {
                stream.Position = 0;
                LargeFileUploadTask<FileAttachment> largeFileUploadTask = new LargeFileUploadTask<FileAttachment>(uploadSession, stream, maxChunkSize);

                await largeFileUploadTask.UploadAsync();
            }
        }
        await graphClient.Users["myemail@mycompany.com"].Messages[msgResult.Id].Send().Request().PostAsync();

}

I try something like this:

var content = new System.Net.Http.Headers.ContentRangeHeaderValue(0,MyFile.Length-1,MyFile.Length);

but i dont now how to asign this content variable, i think that must go in the uploadSession but dont know how.

------------------------------------EDIT------------------------------ included a Picture where see that the size of the attachment is not zero

enter image description here

Ion
  • 549
  • 1
  • 11
  • 25

0 Answers0