12
using Microsoft.Graph
IMessageAttachmentsCollectionPage Message.Attachments

I can not seem to get this to take any "ContentBytes" which is in the FileAttachment.ContentBytes.

My sample is from 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"
            }
        }
};
a_hardin
  • 4,991
  • 4
  • 32
  • 40
twc
  • 449
  • 1
  • 3
  • 12
  • Want to make clear or clear to me that Assembly Microsoft.Graph, Version=1.2.0.0 sets the JSON property for Attachments. So, if you use it, you can't send Attachments using it. :) if I said that correctly because the Microsoft.graph.FileAttachment cannot convert to Microsoft.Graph.IMessageAttachmentsCollectionPage. – twc Feb 21 '17 at 21:52

2 Answers2

19

Using the sample above from GitHub this is resolved, see below:

// 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();
twc
  • 449
  • 1
  • 3
  • 12
  • NOTE: 4MB Limit using this method: https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/message_post_attachments Since there is currently a limit of 4MB on the total size of each REST request, this limits the size of the attachment you can add to under 4MB. – twc Feb 22 '17 at 14:35
4

I'm not quite sure what exactly is going here without seeing a trace of what is being set in the request, an error message, or a http status code. I do know that you can't set the HasAttachments property, that property is only set by the service. Oh, the issue here is that you're setting the Message.Attachments property as a new[] instead of a new MessageAttachmentsCollectionPage. With that said, I just ran the following code and it worked as expected so we know the service will work for this scenario.

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

I hope this helps and saves you time.

Update: This is using Microsoft.Graph.

Michael Mainer
  • 3,387
  • 1
  • 13
  • 32
  • I using Microsoft.graph and it has Attachments – twc Feb 21 '17 at 20:23
  • I'm using Microsoft.Graph and it has [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "attachments", Required = Required.Default)] public IMessageAttachmentsCollectionPage Attachments so I do not see how I can use your attachment.ODataType... Tim: – twc Feb 21 '17 at 20:26
  • 1
    I do not see how your solution is working due that I don't know if your using Microsoft.Graph or using Newtonsoft.Json - I'm using ONLY Microsoft.Graph and it traps you with cannot Implicily convert Microsoft.graph.FileAttachment to Microsoft.graph.IMessageAttachmentsCollectionPage – twc Feb 21 '17 at 20:48
  • Can you please mark your answer as not complete because it is not using Assembly Microsoft.Graph - it must be "using Newtonsoft.Json Assembly. I just want to try and get this answered and yours is stating that it worked but it did not. Many thanks, Tim: – twc Feb 22 '17 at 00:10
  • The answer I provided is using the Microsoft.Graph client library. I didn't show how the ```message``` was created – Michael Mainer Feb 22 '17 at 20:06