0
votes

how can I get the MIME-Content from an ItemAttachment (Message) attached to an email in C#?

In the Graph Explorer it is possible to get the MIME-Content:

https://graph.microsoft.com/v1.0/users/{id}/messages/{id}/attachments/{id}/$value

The Graph Explorer suggests to use ".Content" in C#. But if I try this, the compiler says:

Error CS1061 'IAttachmentRequestBuilder' does not contain a definition for 'Content' and no accessible extension method 'Content' accepting a first argument of type 'IAttachmentRequestBuilder' could be found (are you missing a using directive or an assembly reference?)

C# code:

private async Task<ItemAttachment> GetItemAttachment(string id)
{
    ItemAttachment itemAttachment = null;

    var attachment = await GraphServiceClient
        .Users[CurrentUser.Id]
        .Messages[CurrentMessage.Id]
        .Attachments[id]
        .Content
        .Request()
        .GetAsync();

    itemAttachment = attachment as ItemAttachment;

    return itemAttachment;
}

What is the correct way to get the MIME-content of an attachment in C#?

1

1 Answers

0
votes

The Content property is missing for C#. I don't know the reason.

As an alternative you can try send the http request and read the content from the response.

// create request url
var requestUrl = GraphServiceClient.Users[CurrentUser.Id].Messages[CurrentMessage.Id]
   .Attachments[id].AppendSegmentToRequestUrl("$value");
// create request
var hrm = new HttpRequestMessage(HttpMethod.Get, requestUrl);
// authenticate request
await client.AuthenticationProvider.AuthenticateRequestAsync(hrm);
// send request and read response
var response = await client.HttpProvider.SendAsync(hrm);
// read content
var content = await response.Content.ReadAsStringAsync();