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

Send Mail with attachment Microsoft Graph not working(发送带有附件 Microsoft Graph 的邮件不起作用)

本文介绍了发送带有附件 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"
    })
  }
  ...
}

这篇关于发送带有附件 Microsoft Graph 的邮件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:发送带有附件 Microsoft Graph 的邮件不起作用

基础教程推荐