question

JesperBlochChristiansen-7833 avatar image
0 Votes"
JesperBlochChristiansen-7833 asked saldana-msft edited

Error when trying to send message with attachment

Hi,

I am trying to send a message using the C# graph api client using the following code...

  var draft = await client.Me.Messages                
                     .Request()
                     .AddAsync(message);
    
                 await client.Me.Messages[draft.Id]
                     .Send()
                     .Request()
                     .PostAsync();  

Attachments are added is this way, where file is a
file in memory.

 message.Attachments.Add(new FileAttachment
                 {
                     ContentBytes = file.Content,
                     ContentType = file.ContentType,
                     ContentId = file.Id.ToString(),
                     Name = file.Name,
                     IsInline = false
                 });

But I keep getting this error when attachment is eg. a pdf file. It works fine for emails without attachments or with eg. .png files attached.

 Status Code: BadRequest
 Microsoft.Graph.ServiceException: Code: ErrorUnsupportedTypeForConversion
 Message: Unsupported type for restriction conversion.

Any help would be appreciated :)





microsoft-graph-sdkmicrosoft-graph-mail
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What is the size of the files that fail to attach?

0 Votes 0 ·

So it fails when I attach a single PDF file at about 300 KB
but succeeds when attaching a .png file at about 600KB.

It seems to be related to file type or content, but it is unclear to me what the reason is.

1 Vote 1 ·

1 Answer

Danstan-MSFT avatar image
0 Votes"
Danstan-MSFT answered JesperBlochChristiansen-7833 commented

I cant see what the content type and file name are in your case but I am able to add a pdf with the code below. Check this against what values you have and you should figure out what is wrong. I suspect file content, name or content type of the file in memory. Try with a file that you read like below (SDK version Microsoft.Graph/3.27.0)

 byte[] contentBytes = System.IO.File.ReadAllBytes(@"sample.pdf");
 string contentType = "application/pdf";
 MessageAttachmentsCollectionPage attachments = new MessageAttachmentsCollectionPage();
 attachments.Add(new FileAttachment
 {
     ODataType = "#microsoft.graph.fileAttachment",
     ContentBytes = contentBytes,
     ContentType = contentType,
     ContentId = "sample-pdf",
     Name = "sample-1.pdf"
 });
 Message email = new Message
 {
     Body = new ItemBody
     {
         ContentType = BodyType.Html,
         Content = "They were awesome!"
     },
     Subject = "Old guard talks at Noon",
     ToRecipients = new List<Recipient>()
     {
         new Recipient
         {
             EmailAddress = new EmailAddress
             {
                 Address = "name@domin.onmicrosoft.com"
             }
         }
     },
     Attachments = attachments
 };
    
 // Create the message.
 var draft = await graphServiceClient.Me.Messages.Request().AddAsync(email);
 // Send the message.
 await graphServiceClient.Me.Messages[draft.Id]
             .Send()
             .Request()
             .PostAsync();


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks Danstan, yes this example works fine and helped me track down what is actually going on.

I failed to mention earlier that I am also adding extensions, and if I add extensions to the message in your working example the error tiggers when trying to create the draft message. It works if i'm adding a .png as an attachment or have no attachment, but the combination of eg. a pdf document and extensions seems to fail.

So adding this just before creating the draft message - result in an error when creating the draft message.

  email.Extensions = new MessageExtensionsCollectionPage() {
     new OpenTypeExtension {
         ExtensionName = "com.somecompany",
         AdditionalData = new Dictionary<string, object>()                       
     }
 };


Thanks again

0 Votes 0 ·