代码之家  ›  专栏  ›  技术社区  ›  J Flex

如何从邮件而不是附件中获取文件附件

  •  0
  • J Flex  · 技术社区  · 7 年前

    首先,感谢您花时间阅读我的问题!

    我需要检索邮件附件的“contentbyte”。

    我用 Microsoft.Graph SDK for dotnet . 我检索一条消息,然后获取message.body.content(is html)并将其显示在iframe中。为了显示附件(cid:…),我必须在message.attachments中获取它们。但这是我的问题。邮件附件的fileAttachment类型具有“ContentByte”属性,我可以使用它来显示附件。问题是,SDK没有对message.attachments使用类型“fileattachment”,但“attachment”没有“contentbyte”属性。

    这是我的代码:

    Message data = await graphClient.Me
                    .Messages[messageId]
                    .Request().GetAsync();
    

    var base64 = message.Attachments.Where(c => c.ContentId == contentId).ContentByte;
    

    当我使用调试器浏览“数据”时,我可以看到文件附件中包含所有正确数据的所有字段。但是当我尝试用第二行访问它时,我会在“contentID”下得到一条红线,因为附件的属性不存在。

    这是一个bug,是“message”类中的一个错误,还是必须指定要保留“fileattachment”类型的位置?

    谢谢您!

    1 回复  |  直到 7 年前
        1
  •  0
  •   Vadim Gremyachev    7 年前

    这是预期的行为,因为 Message.Attachments 返回的集合 Attachment type .
    要获取文件附件列表,可以通过 FileAttachment type 通过 OfType Linq method :

    //request message with attachments
    var message = await graphClient.Me
          .Messages[messageId]
          .Request().Expand("Attachments").GetAsync();
    //filter by file attachments and return first one
    var fileAttachment = message.Attachments.OfType<FileAttachment>()
          .FirstOrDefault(a => a.ContentId == contentId);
    
    if (fileAttachment != null)
    {
         var base64 = fileAttachment.ContentBytes;
         //...
    }
    
    推荐文章