2

We're attempting to send emails with csv file attachments via the MS GRAPH API. Our process works fine for emails without attachments, but whenever we attempt to include the csv we receive the following Error 400:

The property 'ContentBytes' does not exist on type 'microsoft.graph.attachment'. Make sure to only use property names that are defined by the type or mark the type as open type.

The code we are using to generate the attachment:

message.Attachments = new Microsoft.Graph.MessageAttachmentsCollectionPage();
message.HasAttachments = true;
byte[] attachmentAsBytes = File.ReadAllBytes(fileDirectory + @"\filename.csv");
string asBase64 = Convert.ToBase64String(attachmentAsBytes);
message.Attachments.Add(new Microsoft.Graph.FileAttachment()
{
    ODataType = "#microsoft.graph.fileAttachment",
    Name = "filename.csv",
    ContentType = "text/csv",
    ContentBytes = Convert.FromBase64String(asBase64)
});

It seems like the API is incorrectly identifying the attachment as type "microsoft.graph.attachment" instead of the marked "microsoft.graph.fileAttachment." What am I missing/failing to understand about the ODataType field or Graph API endpoint?

cWilson
  • 25
  • 5

1 Answers1

0

Avoid to set ODataType property. When a default constructor of FileAttachment class is called the correct value of ODataType property is set.

According this the correct ODataType is microsoft.graph.fileAttachment, not #microsoft.graph.fileAttachment

user2250152
  • 14,658
  • 4
  • 33
  • 57
  • 1
    Unfortunately we've attempted both of the suggestions, letting the ODataType be set automatically by the constructor, and setting the string without the #. Both result in the same error 400. – cWilson Aug 15 '22 at 11:41
  • @cWilson Could you try to not convert bytes to base64? Simply set ContentBytes=File.ReadAllBytes(fileDirectory + @"\filename.csv") – user2250152 Aug 15 '22 at 12:31
  • This was our original approach when we first got the E400. The solutions I found all pointed to the content being in base64, so we made the conversion hoping that the error message was incorrect (that the content was the problem, not the data type label). – cWilson Aug 15 '22 at 13:32