【问题标题】:file attachment using microsoft graph send mail not working使用 microsoft graph 发送邮件的文件附件不起作用
【发布时间】:2019-11-20 07:12:50
【问题描述】:

获取不带附件的邮件 我正在使用 microsoft graph 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--");
});
}

我在这里错过了什么??

【问题讨论】:

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


    【解决方案1】:

    这很可能是由于来自request documentation的无效负载请求而发生的

    body - PATCH、POST 和 PUT 请求的实体主体。必须是一个 缓冲区、字符串或读取流。如果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 设置为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
      };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-08
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-02
      • 1970-01-01
      相关资源
      最近更新 更多