title: Send Mail with attachment via Powershell Graph API
body: <p>I'm currently trying to send a mail using an automated Powershell script using the GraphAPI from Microsoft.</p>
  
  <p>Here the full story : I need to check every 15 min the mail received on an Office 365 mailbox, get a specific file attachment (filter by is name), download the file, and re-send it with a new mail.</p>
  
  <p>Everything working but I canno't send the new mail with the attachment ( Works without the attachment).</p>
  
  <p>Here my code : </p>
  
  <pre><code>    $date = (get-date).ToUniversalTime().AddMinutes(-15).ToString("yyyy-MM-ddTHH:mm:ssZ")
      $Uri = "https://graph.microsoft.com/v1.0/me/messages"
      $UriSend = "https://graph.microsoft.com/v1.0/me/sendMail"
      $filter = "?`$select=Id,ReceivedDateTime&amp;`$filter=HasAttachments eq true and ReceivedDateTime ge " + $date 
      $url = $Uri + $filter
  
      $Result = (Invoke-RestMethod -Method Get -Headers $requestheader -Uri $url)
  
      if($Result.value){ 
      ## Loop through each results 
      foreach ($message in $Result.value) 
      { 
          # get attachments and save to file system 
          $query = $Uri + "/" + $message.Id + "/attachments" 
          $attachments = (Invoke-RestMethod -Method Get -Headers $requestheader -Uri $query)
  
          # in case of multiple attachments in email 
          foreach ($attachment in $attachments.value) 
      { 
          $patternPDF = "FAX AR document\.pdf$"
          if($attachment.Name -match $patternPDF)
          {
              $name = $message.ReceivedDateTime -replace "T","-"
              $name = $name -replace "Z",""
              $name = $name -replace ":","-"
              $path = "c:\Temp\" + $name + ".pdf"
  
              # Creation of the PDF file
              $Content = [System.Convert]::FromBase64String($attachment.ContentBytes) 
              Set-Content -Path $path -Value $Content -Encoding Byte
  
  
              $file = Get-Content $path -Encoding Byte -ReadCount 0
  
              #Send File by Mail
              $body = 
                  @"
                  {
                  "message" : {
                      "subject": "AR Fax",
                      "body" : {
                          "contentType": "Text",
                          "content": "Accusé Fax"
                      },
                      "toRecipients": [
                      {
                          "emailAddress" : {
                              "address" : "mail@domain.com"
                           }
                      }
                      ],
                      "attachments":[
                          {
                              "@@odata.type":"#microsoft.graph.fileAttachment",
                              "name":"Fax_AR.pdf",
                              "contentType":"application/pdf",
                              "contentBytes":"$file"
                           }
                      ]
                  },
                  "saveToSentItems": "true"
                  }
  "@
              # Invokes the request
              Invoke-RestMethod -Headers $requestheader -Uri $uriSend -Method Post -Body $body 
  </code></pre>
  
  <p>I'm not very good in powershell (or in code generally) so please be indulgent^^</p>
  
  <p>EDIT : I've find another way to do what i want (I print the file now)</p>