使用MS Graph发带附件邮件时遇MessageAttachmentsCollectionPage不存在错误
解决方法:适配Microsoft Graph SDK 5.x版本
你遇到的错误是因为Microsoft Graph SDK 5.x已经移除了MessageAttachmentsCollectionPage类型,不需要额外添加程序集,只需要修改代码适配新版SDK的集合类型规范即可。
修改后的代码:
// Create the message with attachment. byte[] contentBytes = System.IO.File.ReadAllBytes(@"C:\test\test.png"); string contentType = "image/png"; // 直接用List<Attachment>替代MessageAttachmentsCollectionPage List<Attachment> attachments = new List<Attachment>(); attachments.Add(new FileAttachment { ODataType = "#microsoft.graph.fileAttachment", ContentBytes = contentBytes, ContentType = contentType, ContentId = "testing", Name = "testing.png" }); Message email = new Message { Body = new ItemBody { Content = Resource.Prop_Body + guid, ContentType = BodyType.Text, }, Subject = Resource.Prop_Subject + guid.Substring(0, 8), ToRecipients = recipients, Attachments = attachments }; // Send the message. await graphClient.Me.SendMail(email, true).PostAsync(); // 注意:5.x SDK里不再需要Request()方法,直接调用PostAsync即可
补充说明:
- Graph SDK 5.x对API调用方式和类型做了简化,集合类型统一使用.NET标准集合(如
List<T>),不再依赖旧版的*CollectionPage类 - 发送邮件的调用逻辑也做了精简,移除了
.Request()链式调用,直接调用.PostAsync()即可完成请求
内容的提问来源于stack exchange,提问作者CDVS
相关产品推荐
相关产品推荐

