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 である必要があります。が true の場合json、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
  };
于 2019-11-20T09:21:48.900 に答える