在 C# 中使用 REST 示例的图形 API SendMail 和附件

use*_*125 2 microsoft-graph-api

Graph API Send Email文档指出我可以在同一个 sendMail 操作调用中包含文件附件。有人能够提供一个示例代码,说明如何使用 me/sendmail 以及生成的文件附件(内容存储在内存流中)来实现这一点吗?

Ale*_*lex 5

一种选择是使用Microsoft Graph .NET SDK ,我在另一个Stack Overflow 答案中找到了此示例代码。

// 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();
Run Code Online (Sandbox Code Playgroud)