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

我正在制作一个应用程序,如 通过这篇文章.

I am making an application that sends emails from a User as described by this article.

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

Everything is working as expected, except for when I try to include an attachment. The email sends, but without the attachment. I'm not sure what the problem is as I've tried pretty much everything I could find online. I have made sure the file I am sending is properly encoded in 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 内部,而不是外部.这应该有效:

Attachments go inside the message JSON, not outside of it. This should work:

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

相关文章