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。
- 将BigInteger转换为十进制(Base 10)字符串的最快方法?
- 如何使用AutoMapper基于展平属性的名称查找源属性
- 为什么我的WebClient上传文件代码挂起?
- ASP.NET Core和Angular 4入门在Visual Studio 2015中使用WEB API
- 是否有Int.isWholeNumber()函数或类似的东西?
- Windows Forms,DB驱动的应用程序开发/原型设计是否有任何框架?
- MVVM,对ViewModelLocator和DataTemplate感到困惑,首先导致ViewFirst与ViewModel
- 更快的替代品.Distinct()
- 我可以在没有Visual Studio的情况下编译DLL吗?