Microsoft.Graph发送带附件的邮件

时间:2017-02-21 17:53:41

标签: c# microsoft-graph

using Microsoft.Graph
IMessageAttachmentsCollectionPage Message.Attachments

我似乎无法接受任何" ContentBytes"它位于FileAttachment.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"

                    }

                }
        };

2 个答案:

答案 0 :(得分:2)

使用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();

答案 1 :(得分:1)

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

REPLACE(STR(FieldName,6),' ','0')

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

更新:这是使用Microsoft.Graph。