javascript - 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"
})
}
...
}
推荐阅读
- excel - 创建自定义编号格式 Excel
- flutter - 如何在图像中添加缩放?
- wpf - 使用 MVVM 和 Caliburn Micro 绑定对象的属性
- ios - ionic2.2+cordova 8.1 iOS:能写文件但不能读文件
- firebase - firebase - 如何将软件安装到已部署到的服务器上?
- android - Ionic/Cordova 应用程序找不到 MainActivity
- python - 如何检查 int 输入是否在列表中?
- regex - Linux Bash grep 从匹配字符串中提取单词
- sql - 基于 WHERE 子句的 Liquibase 级联删除多对多
- python - Python - 6 个或更多尾部/头部代码的概率