我正在尝试使用MS Graph 1.0发送电子邮件,但没有任何结果或响应。尚未发送电子邮件,sendMail方法未返回任何错误消息……它只显示“空”。
我的代码基于此示例https://github.com/microsoftgraph/msgraph-sdk-javascript#post-and-patch,如下所示:
// 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);
}
我想mail var需要一个标头,但是无论如何,API应该给我返回一些信息,对吧?而且,显然,这是电子邮件发送的问题吗?
答案 0 :(得分:0)
我终于将rawResponse添加到.post调用中,并查看错误日志...
client
.api('/me/sendMail')
.header("Content-type", "application/json")
.post({message: mail}, (err, res, rawResponse) => {
console.log(rawResponse);
console.log(err);
});
...,我可以看到认证令牌有问题。因此,我正确使用了api,问题代码也没问题。