0
votes

I'm trying to send e-mails with MS Graph 1.0 and I have not any get any result or response. E-Mails haven't been sent and sendMail method don't return any error o message... it only says "null".

My code is based on this example https://github.com/microsoftgraph/msgraph-sdk-javascript#post-and-patch and looks like this:

// Initialize Graph client
  const client = graph.Client.init({
    authProvider: (done) => {
      done(null, accessToken);
    }
  });

  try {
    // construct the email object
    var mail = {
        subject: "Microsoft Graph JavaScript Sample",
        toRecipients: [{
            emailAddress: {
                address: "mail@domain.com"
            }
        }],
        body: {
            content: "<h1>MicrosoftGraph JavaScript Sample</h1>Check out https://github.com/microsoftgraph/msgraph-sdk-javascript",
            contentType: "html"
        }
    };

    client
        .api('/me/sendMail')
        .post({message: mail}, (err, res) => {
            console.log("---> " + res);
        });

    console.log("Try ends");

  } catch (err) {
    parms.message = 'Error retrieving messages';
    parms.error = { status: `${err.code}: ${err.message}` };
    parms.debug = JSON.stringify(err.body, null, 2);
    res.render('error', parms);
  }

I guess mail var needs a header, but anyway, API should return me something, right? And, obviously, which is the problem with the email sending?

1

1 Answers

0
votes

I finally added rawResponse to .post call and look at err log...

client
  .api('/me/sendMail')
  .header("Content-type", "application/json")
  .post({message: mail}, (err, res, rawResponse) => {
      console.log(rawResponse);
      console.log(err);
});

... and I could see that I had problem with my authentication token. So, I was using the api correctly and code from the question is ok.