Microsoft.Graph如何发送带附件的邮件

13
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"
            }
        }
};

想要向我澄清或解释一下,Assembly Microsoft.Graph,Version=1.2.0.0设置了附件的JSON属性。因此,如果您使用它,就不能使用它发送附件。:) 如果我说得正确的话,因为Microsoft.graph.FileAttachment无法转换为Microsoft.Graph.IMessageAttachmentsCollectionPage。 - twc
2个回答

20

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

请注意:使用此方法,限制为4MB:https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/message_post_attachments 由于当前每个REST请求的总大小限制为4MB,因此这限制了您可以添加的附件大小不超过4MB。 - twc

4

没有看到请求中设置的跟踪信息、错误信息或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。

我正在使用 Microsoft.graph,它具有附件。 - twc
我正在使用Microsoft.Graph,它有[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "attachments", Required = Required.Default)] public IMessageAttachmentsCollectionPage Attachments,所以我不知道如何使用你的attachment.ODataType... Tim: - twc
1
我看不出你的解决方案是如何工作的,因为我不知道你是使用Microsoft.Graph还是使用Newtonsoft.Json - 我只使用Microsoft.Graph,并且它会陷入无法将Microsoft.graph.FileAttachment隐式转换为Microsoft.graph.IMessageAttachmentsCollectionPage的困境。 - twc
请问您能否将您的答案标记为未完成,因为它没有使用Assembly Microsoft.Graph - 必须是"using Newtonsoft.Json Assembly。我只是想尝试并得到答案,而您的回答表明它可以工作,但实际上并没有。非常感谢,Tim。 - twc
我提供的答案使用了Microsoft.Graph客户端库。我没有展示如何创建message - Michael Mainer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接