Microsoft.Graph 发送带附件的邮件
Microsoft.Graph send mail with attachment
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"
}
}
};
如果没有看到请求中设置的内容、错误消息或 http 状态代码的踪迹,我不太确定这里到底发生了什么。我知道您无法设置 HasAttachments 属性,属性 仅由服务设置。哦,这里的问题是您将 Message.Attachments 属性 设置为 new[] 而不是 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.
使用上述 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();
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"
}
}
};
如果没有看到请求中设置的内容、错误消息或 http 状态代码的踪迹,我不太确定这里到底发生了什么。我知道您无法设置 HasAttachments 属性,属性 仅由服务设置。哦,这里的问题是您将 Message.Attachments 属性 设置为 new[] 而不是 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.
使用上述 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();