Error when trying to send message with attachment

Jesper Bloch-Christiansen 6 Reputation points
2021-03-31T13:28:45.1+00:00

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
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,716 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Danstan Onyango 3,741 Reputation points Microsoft Employee
    2021-04-01T11:20:16.26+00:00

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