使用Microsoft图形的文件附件发送邮件不起作用

时间:2019-11-20 07:12:50

标签: javascript node.js microsoft-graph microsoft-graph-mail exchange-basicauth

获取不带附件的邮件 我正在使用Microsoft图sendMail。 我需要同时添加附件。 我在请求正文的消息中添加了附件对象。 但是没有附上收到邮件。 我正在关注: https://docs.microsoft.com/en-us/graph/api/resources/fileattachment?view=graph-rest-1.0 PFB我的代码。

function sendAttachment(accessToken) {
  const attachments = [
    {
      "@odata.type": "#microsoft.graph.fileAttachment",
      "contentBytes": "",
      "name": "example.jpg"
    }
  ];
  var message= 
      { subject: 'It\'s working ',
        body: 
         { contentType: 'Text',
           content: 'Sending mail using microsoft graph and Outh2.0' },
        toRecipients: [ { emailAddress: { address: '' } } ],
        ccRecipients: [ { emailAddress: { address: '' } } ] 
      };

  message["attachments"] = attachments;

  var options = { 
  method: 'POST',
  url: 'https://graph.microsoft.com/v1.0/users/xyz@xyz.com/sendMail',
  headers: 
   { 'Cache-Control': 'no-cache',
     Authorization: 'Bearer '+ accessToken,
     'Content-Type': 'application/json' },
  body:JSON.stringify({
      "message": message, 
      "SaveToSentItems": "false"
    }),
  json: true
   };

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log("--attachment--");
});
}

我在这里想念什么?

1 个答案:

答案 0 :(得分:0)

最有可能是由于来自request documentation的有效负载请求无效

  

body-用于PATCH,POST和PUT请求的实体主体。必须是   缓冲区,字符串或ReadStream。如果json为true,则body必须为   JSON可序列化的对象。

因此,json选项之一应省略:

const options = {
    method: "POST",
    url: `https://graph.microsoft.com/v1.0/users/${from}/sendMail`,
    headers: {
      "Cache-Control": "no-cache",
      Authorization: "Bearer " + accessToken,
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      message: message,
      SaveToSentItems: "true"
    })
  };

json设置为true,但将body指定为JSON对象:

const options = {
    method: "POST",
    url: `https://graph.microsoft.com/v1.0/users/${from}/sendMail`,
    headers: {
      "Cache-Control": "no-cache",
      Authorization: "Bearer " + accessToken,
      "Content-Type": "application/json"
    },
    body: {
      message: message,
      SaveToSentItems: "true"
    },
    json: true
  };