microsoftgraph/aspnet-snippets-sample

Send Mail with Attachment - no error Sent Succeful but no attachment

twcarnahan opened this issue · 7 comments

Using the sample and working in the model MailService.cs,
I added HasAttachments = true,
I pass my Filename and FileData into the JSON like below:
while (rdr0.Read())
{
FileAttachment attachments = new FileAttachment
{
ODataType = "#microsoft.graph.fileAttachment",
Name = rdr0["FileName"].ToString(),
ContentBytes = rdr0["FileData"] as byte[]
};
}

   // Send the message.
    await graphClient.Me.SendMail(email, true).Request().PostAsync();

====
here is another attempt:
FileAttachment fileAttachment = new FileAttachment();

          {
            fileAttachment.ODataType = "#microsoft.graph.fileAttachment";
            fileAttachment.ContentBytes = rdr0["FileData"] as byte[];
            fileAttachment.Name = rdr0["FileName"].ToString();
        };

I get the message but don't get the attachment.
Any direction on sending the message with attachment?
Vr, Tim:
I must be missing something in the
Microsoft.Graph.IMessage.Attachments.CollectionPage
because I can not get anything converted or cast into it -- Thanks for the direction, Tim:

It appears that using Microsoft.Graph assemble already has the JSON property Attachment which has all the necessary properties Odata,Name,ContentBytes,
However, FileAttachment defines Byte [] ContentBytes --- but, FileAttachment can not be implicitly converted to IMessageAttachmentsCollectionPage.

Just need so help to sort this out - many thanks, Tim:

I posted this on stack flow as you ask last time but did receive a good answer although I thought I would share. Tim:
http://stackoverflow.com/questions/42374501/microsoft-graph-send-mail-with-attachment

Hi Tim. I was able to send an email with an attachment based on the examples on Stack Overflow. It sounds like you just need to create an attachments collection (as a MessageAttachmentsCollectionPage object), add your file attachment to the attachments collection, and then assign the attachments collection to the message's Attachments property. You don't need to convert your file attachment.

Here's the code that I used:

// Create the message with attachment.
byte[] contentBytes = System.IO.File.ReadAllBytes(@"C:\Users\dianed\Pictures\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();

This example--and Michael's example on Stack Overflow--use the Microsoft.Graph client library. This doesn't account for your questions regarding the JSON property for attachments, but I'm not sure that you are deliberately trying to use it. ??

Diane, I was able to get this to work as well. Yes, it does answer my JSON property question as the JSON property for attachments is set in the Public Message Class if you use Microsoft.Graph Assembly. IMichael's example must not be using the Microsoft.Graph Assembly but just using the URI with Newton.Json Assembly which works as you have another example REST snippets that does this.
I will try to put this in another test tomorrow and I'll post the results as I believe maybe you should add this to your sample and clarify that your "using Microsoft.Graph" and Not "using Newton.Json" Assembly
NOTE: if you do use both, you will get a conflict setting Mail Class and Attachment JSON Properties.
Thank you again for your knowledge and direction. Tim:
I hope I didn't mix up anything and if so - please alert me -again many thanks, Tim:

Yes, please let us know your results. I plan to add this to the sample soon (along with snippets about retrieving messages with attachments). I'll look out for a JSON conflict and make sure to note that. Thanks again for your feedback!

the conflict is here in your other sample:
https://github.com/microsoftgraph/aspnet-connect-rest-sample/blob/master/Microsoft%20Graph%20REST%20ASPNET%20Connect/Microsoft%20Graph%20REST%20ASPNET%20Connect/Models/GraphResources.cs
Because you haven't put anything for "Attachments" you can be safe but you can't use both --- the MODEL and Microsoft.Graph.
Maybe I'm telling you something that is academic but from what I realized from research, many don't know that Micrsoft.Graph has the JSON properties already loaded --- or maybe they do but I think the stackflow answer doen't use Microsoft.Graph but only "using Newtonsoft.Json" as you did in above REST sample.
Please advise if I got this all mixed up -- Much Appreciated DianeD, Tim:

I used the above and it works and appreciate the time and knowledge - Tim: