0

Getting the mail without the attachment I am using microsoft graph sendMail. I need to add an attachment at the same time. I added the attachment Object inside message of request body. But received the mail without the attaachment. i was following : https://docs.microsoft.com/en-us/graph/api/resources/fileattachment?view=graph-rest-1.0 PFB my code.

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--");
});
}

what am i missing here ??

4

1 回答 1

0

它很可能是由于无效的有效负载请求而发生的,来自request文档

body- PATCH、POST 和 PUT 请求的实体主体。必须是 Buffer、String 或 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设置为truebody指定为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
  };
于 2019-11-20T09:21:48.900 回答