NodeJs Graph sendMail

Ruben Teevin 11 Reputation points
2022-06-29T23:52:53.983+00:00

I'm trying to implement a function to send Emails using the graph API.

require('isomorphic-fetch');  
const { Client } = require('@microsoft/microsoft-graph-client');  
const { TokenCredentialAuthenticationProvider } = require('@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials');  
const { ClientSecretCredential } = require('@azure/identity');  
  
const tenantId = 'xx';  
const clientId = 'xx';  
const clientSecret = 'xx';  
  
const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);  
const authProvider = new TokenCredentialAuthenticationProvider(credential, {  
    scopes: ['https://graph.microsoft.com/.default']  
});  

const client = Client.initWithMiddleware({  
    debugLogging: true,  
    authProvider  
});  

I tried sending "invitations" and it works

client.api('/invitations').post(invitation)  

But for sending emails

 client.api('/users/<My-User-Principal-Name>/sendMail')  

when<My-User-Principal-Name> is my email Im getting this error

{"code":"Request_BadRequest","message":"Specified HTTP method is not allowed for the request target."...}  

and when its my ID, I get this

{"error":{"code":"OrganizationFromTenantGuidNotFound","message":"The tenant for tenant guid..."}}  

Help please :)

Microsoft Security Microsoft Graph
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.