Send email with attachment - MS Graph

Venildo Amaral 1 Reputation point
2020-10-23T21:31:31.307+00:00

good night
I am sending email without user authentication, automatic method, this is working, but I would like to attach several files to the email, how to do this?
I am using the following code to send email.

{"message": {
   "subject": "xxxxxx",
   "body": {
     "contentType": "HTML", "content": "yyyyyyyyyyyyy"},
     "toRecipients": [
       {
         "emailAddress": {
           "address": "ttttt@gmail.com"}
        }
      ]
    }
}

POST https://graph.microsoft.com/v1.0/users/{id}/sendMail
Content-Type application/json
Authorization Bearer " & Token
Host graph.microsoft.com"

I know a parameter called attachment, but I was unable to use it pointing a physical file path, I need to point several files to be sent in the email.
How to make?

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,521 questions
Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,335 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Siddharth Sharma 6 Reputation points
    2021-12-07T14:08:58.53+00:00

    @Venildo Amaral

    The solution would be to add array of attachments(since you want add multiple number of attachments) like -
    "attachments": [
    {
    "@odata.type": "#microsoft.graph.fileAttachment",
    "name": "Teste de Anexo",
    contentType: 'text/plain',
    contentBytes: byteContentOfFile,
    },
    {
    "@odata.type": "#microsoft.graph.fileAttachment",
    "name": "File2.txt",
    contentType: 'text/plain',
    contentBytes: byteContentofFile2,
    },
    {
    "@odata.type": "#microsoft.graph.fileAttachment",
    "name": "File3.txt",
    contentType: 'text/plain',
    contentBytes: byteContentofFile3,
    },
    ]

    Hope the solution works for you !!!

    0 comments No comments