【问题标题】:Microsoft Graph Rest API: How to get MIME-Content from an ItemAttachment in C#Microsoft Graph Rest API:如何从 C# 中的 ItemAttachment 获取 MIME 内容
【发布时间】:2021-06-17 01:51:15
【问题描述】:

如何从 C# 中附加到电子邮件的 ItemAttachment(消息)中获取 MIME 内容?

在 Graph Explorer 中可以获取 MIME 内容:

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

Graph Explorer 建议在 C# 中使用“.Content”。但是如果我尝试这个,编译器会说:

错误 CS1061 'IAttachmentRequestBuilder' 不包含定义 对于“内容”并且没有可访问的扩展方法“内容”接受 可以找到“IAttachmentRequestBuilder”类型的第一个参数(是 您缺少 using 指令或程序集引用?)

C#代码:

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;
}

在 C# 中获取附件的 MIME 内容的正确方法是什么?

【问题讨论】:

    标签: c# microsoft-graph-api


    【解决方案1】:

    C# 缺少 Content 属性。不知道是什么原因。

    作为替代方案,您可以尝试发送 http 请求并从响应中读取内容。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-07
      • 1970-01-01
      相关资源
      最近更新 更多