Microsoft.Graph发送带附件的邮件

using Microsoft.Graph IMessageAttachmentsCollectionPage Message.Attachments 

我似乎无法使用FileAttachment.ContentBytes中的任何“ContentBytes”。

我的示例来自Microsoft https://github.com/microsoftgraph/aspnet-snippets-sample 。

  // Create the message. 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, HasAttachments = true, Attachments = new[] { new FileAttachment { ODataType = "#microsoft.graph.fileAttachment", ContentBytes = contentBytes, ContentType = contentType, ContentId = "testing", Name = "tesing.png" } } }; 

使用GitHub上面的示例解决了这个问题,见下文:

 // Create the message with attachment. byte[] contentBytes = System.IO.File.ReadAllBytes(@"C:\test\test.png"); string contentType = "image/png"; MessageAttachmentsCollectionPage attachments = new MessageAttachmentsCollectionPage(); 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).Request().PostAsync(); 

如果没有看到请求中的设置,错误消息或http状态代码的痕迹,我不太确定这里到底发生了什么。 我知道您无法设置HasAttachments属性,该属性仅由服务设置。 哦,这里的问题是你将Message.Attachments属性设置为new []而不是新的MessageAttachmentsCollectionPage 。 话虽如此,我只是运行以下代码,它按预期工作,所以我们知道该服务将适用于这种情况。

  var message = await createEmail("Sent from the MailSendMailWithAttachment test."); var attachment = new FileAttachment(); attachment.ODataType = "#microsoft.graph.fileAttachment"; attachment.Name = "MyFileAttachment.txt"; attachment.ContentBytes = Microsoft.Graph.Test.Properties.Resources.textfile; message.Attachments = new MessageAttachmentsCollectionPage(); message.Attachments.Add(attachment); await graphClient.Me.SendMail(message, true).Request().PostAsync(); 

我希望这有助于节省您的时间。

更新:这是使用Microsoft.Graph。