javascript - 发送带有 Microsoft Graph 附件的邮件不起作用

标签 javascript microsoft-graph-api outlook-restapi

我正在制作一个从用户发送电子邮件的应用程序,如 by this article 所述.

一切都按预期工作,除了我尝试包含附件时。电子邮件已发送,但没有附件。我不确定问题出在哪里,因为我已经尝试了几乎所有可以在网上找到的东西。我已确保我发送的文件已正确编码为 base64。

var message = {
    "subject": subject,
    "hasAttachments":true,
    "body": {
        "contentType": "Text",
        "content": emailBody
    }, 
    "toRecipients": toRecipients,
    ccRecipients, 
    bccRecipients
};


function sendMailRequest(access_token, message, uriSend, file, base64, callback){
const attachments = [{
'@odata.type': '#microsoft.graph.fileAttachment',
"contentBytes": base64
"name": "example.jpg"
}];

// Configure the request
var options2 = {
"url": uriSend,
"method": 'POST',
"headers": { 
    'Authorization': access_token,
    'Content-Type': 'application/json'
},
"body": JSON.stringify({
    "message": message, 
    "SaveToSentItems": "true",
    "Attachments": attachments
})
}

最佳答案

附件位于 message JSON 内部,而不是外部。这应该有效:

function sendMailRequest(access_token, message, uriSend, file, base64, callback) {
  const attachments = [
    {
      "@odata.type": "#microsoft.graph.fileAttachment",
      "contentBytes": base64
      "name": "example.jpg"
    }
  ];

  message["attachments"] = attachments;

  // Configure the request
  var options2 = {
    "url": uriSend,
    "method": "POST",
    "headers": { 
      "Authorization": access_token,
      "Content-Type": "application/json"
    },
    "body": JSON.stringify({
      "message": message, 
      "SaveToSentItems": "true"
    })
  }
  ...
}

关于javascript - 发送带有 Microsoft Graph 附件的邮件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51087270/

相关文章:

javascript - 覆盖 png 图像并在悬停时使图像变暗

javascript - 在 NodeJS 中将 K,V 对分配给 JSON 对象

javascript - 如何从 Microsoft graph API 访问用户的个人资料照片并显示 React 应用程序?

microsoft-graph-api - OneDrive 未配置

angularjs - 读取其他用户时 Microsoft Graph api 403 访问被拒绝

microsoft-graph-api - MS Graph Bearer Token 能否用于访问 Office REST API?

javascript - 多个 Assets 向同一文件名 index.html 发出不同的内容

javascript - 如何在 Mustache 模板中使用 jQuery 内置函数

microsoft-graph-api - Microsoft Graph - 找不到邮箱

icalendar - 从不同的日历中识别相同/相关的日历事件