简体   繁体   中英

Send Mail with attachment Microsoft Graph not working

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM