Your message wasn't delivered because the recipient's email provider rejected it.

System Testing 1 Reputation point
2023-01-06T07:54:05.733+00:00

I am trying to send email using microsoft graph api using powershell script, but the emails are not getting delivered, Its failing with error.
Your message wasn't delivered because the recipient's email provider rejected it.

Get File Name and Base64 string

$FileName=(Get-Item -Path $Attachment).name
$base64string = [Convert]::ToBase64String([IO.File]::ReadAllBytes($Attachment))

Connect to GRAPH API

$tokenBody = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
Client_Id = $clientId
Client_Secret = $clientSecret
}
$tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token" -Method POST -Body $tokenBody
$headers = @{
"Authorization" = "Bearer $($tokenResponse.access_token)"
"Content-type" = "application/json"
}

Send Mail

$URLsend = "https://graph.microsoft.com/v1.0/users/$MailSender/sendMail"
$BodyJsonsend = @"
{
"message": {
"subject": "Hello World from Microsoft Graph API",
"body": {
"contentType": "HTML",
"content": "This Mail is sent via Microsoft <br>
GRAPH <br>
API<br>
and an Attachment <br>
"
},

                      "toRecipients": [  
                        {  
                          "emailAddress": {  
                            "address": "$Recipient"  
                          }  
                        }  
                      ]  
                      ,"attachments": [  
                        {  
                          "@odata.type": "#microsoft.graph.fileAttachment",  
                          "name": "$FileName",  
                          "contentType": "text/plain",  
                          "contentBytes": "$base64string"  
                        }  
                      ]  
                    },  
                    "saveToSentItems": "false"  
                  }  

"@

Invoke-RestMethod -Method POST -Uri $URLsend -Headers $headers -Body $BodyJsonsend

heres core part of script.

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
{count} votes

1 answer

Sort by: Most helpful
  1. Gopinath Chennamadhavuni 2,426 Reputation points
    2023-01-09T06:50:32.557+00:00

    Hi @System Testing ,

    Thanks for reaching out.

    Generally, these issues occur while using trail tenant to perform this send email activity using Graph API. Could you please share the complete NDR information you received, note to clear your personal information.

    For example, if you receive the code like Remote Server returned ‘550 5.7.520 Access denied, Your organization does not allow external forwarding.
    Then you will need to enable the forwarding.

    This error occurs when sending email from known, low reputation IP addresses that are typically used by new customers on Trail tenants.
    To fix this issue your Tenant needs to be added to a sending exclusion for 60 days to allow them to use blocked IPs whilst they still have a new/trial license.

    If that's not your case, please refer to this: How to set up a multifunction device or application to send email using Microsoft 365 or Office 365
    You could check which option you are using now, make sure you have meet all needs to use this option.
    Or

    Please raise a case with MS Support team from your tenant where you are facing issue, respective team will provide exclusion for your tenant.

    Hope this helps.

    If the answer is helpful, please click "Accept Answer" and kindly upvote. If you have further questions about this answer, please click "Comment".

    0 comments No comments