我正在制作一个从用户发送电子邮件的应用程序,如 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/