Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft.Graph send mail with attachment

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"
            }
        }
};
like image 389
twc Avatar asked Feb 21 '17 17:02

twc


People also ask

Is Microsoft Graph being deprecated?

Azure Active Directory (Azure AD) Graph is deprecated and will be retired at any time after June 30, 2023, without advance notice, as we announced in September, 2022.

Is Microsoft Graph API paid?

Microsoft Graph Data Connect consumption charges are billed monthly on a pay-as-you-go basis. Charges are calculated using a flat rate based on the count of per-1,000 objects extracted through the connector.

What is Microsoft Graph email?

Microsoft Graph lets you simply include app data as Internet message headers when creating or sending a new message, or a reply to a message. If you need to add and subsequently update custom data, you can store the data in individual resource instances.

What can you do with Microsoft graphs?

Microsoft Graph is a RESTful web API that enables you to access Microsoft Cloud service resources. After you register your app and get authentication tokens for a user or service, you can make requests to the Microsoft Graph API.


2 Answers

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();
like image 164
twc Avatar answered Oct 11 '22 22:10

twc


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.

like image 34
Michael Mainer Avatar answered Oct 11 '22 22:10

Michael Mainer