Microsoft Graph 发送带附件的电子邮件
Microsoft graph send email with attachments
我正在使用 Microsoft Graph 的一项服务来发送带有附件的电子邮件。
但是当我发送邮件时,这没有我设置的附件。
这是我生成的 Json
` message: {
attachments: attachments[],
subject: Email.Subject,
body: {
contentType: "HTML",
content: Email.body
},
toRecipients: [
{
emailAddress: {
address: Email.To
}
}
],
},
saveToSentItems: true
}
这是我的附件数组
0: {@odata.type: "#microsoft.graph.fileAttachment", contentBytes: "iVBORw0KGgoAAAANSUhEUgAAAPwA…JkiRJkiRJkiRJkiQZ4f8B1nomcWdNLuoAAAAASUVORK5CYII=", name: "outbound.png"}
1: {@odata.type: "#microsoft.graph.fileAttachment", contentBytes: "iVBORw0KGgoAAAANSUhEUgAAAQAA…eGOdrvC6af95tuTmRRrb4fxZWJvYuBoVJAAAAAElFTkSuQmCC", name: "inbound.png"}
`
这是我使用 api 发送邮件的方式
sendMail: async function(accessToken, email) {
const client = getAuthenticatedClient(accessToken);
const sentResult = await client.api('/users/{tenantid}/sendMail').post(email);
}
问题是,邮件已发送,但为什么没有附件
这就是我阅读文件的方式
var attachments = [];
function addAttachments() {
allFiles.forEach(a => {
let reader = new FileReader();
reader.readAsDataURL(a);
reader.onload = function() {
attachments.push({
'@odata.type': "#microsoft.graph.fileAttachment",
name: a.name,
contentType: a.type,
contentBytes: reader.result.split(',')[1],
});
};
})}
这里是邮件对象的控制台日志
email_object
这是我将对象字符串化后的结果
{"message":{"subject":"[AU1588259832480]-random subject","body":{"contentType":"HTML","content":"<p>body test</p>"},"toRecipients":[{"emailAddress":{"address":"email@test.com"}}],"internetMessageId":"AU1588259832480","attachments":[]}}
attachmen 对象是空的,但为什么?
您的 contentBytes
不应包含 data:image/png;base64
序言。那应该只是您图像中的 base64 编码字节。
"attachments": [
{
"@odata.type": "#microsoft.graph.fileAttachment",
"contentBytes": "/9j/4AAQSkZJRgABAQEAYAB...",
"name": "outbound.png",
"contentType": "image/png"
}
]
这是我解决空附件的方法,基于此post
im new posting on stack overflow
function getBase64(file, onLoadCallback) {
return new Promise(function(resolve, reject) {
var reader = new FileReader();
reader.onload = function() { resolve(reader.result); };
reader.onerror = reject;
reader.readAsDataURL(file);
});
}
const Send = {
message: {
subject: `[${quoteNumb ? quoteNumb : quoteHomeNumber}]-${Email.subject}`,
body: {
contentType: "HTML",
content: Email.body
},
toRecipients: [],
attachments: []
}};
async function sendMail() {
for (const a of allFiles) {
var fileData = await getBase64(a).catch(err => {
console.log(err)
});
Send.message.attachments.push({
'@odata.type': "#microsoft.graph.fileAttachment",
name: a.name,
contentType: a.type,
contentBytes: fileData.split(',')[1]
});
}
graph.sendMail(AccesToken, Send);}
我正在使用 Microsoft Graph 的一项服务来发送带有附件的电子邮件。 但是当我发送邮件时,这没有我设置的附件。 这是我生成的 Json
` message: {
attachments: attachments[],
subject: Email.Subject,
body: {
contentType: "HTML",
content: Email.body
},
toRecipients: [
{
emailAddress: {
address: Email.To
}
}
],
},
saveToSentItems: true
}
这是我的附件数组
0: {@odata.type: "#microsoft.graph.fileAttachment", contentBytes: "iVBORw0KGgoAAAANSUhEUgAAAPwA…JkiRJkiRJkiRJkiQZ4f8B1nomcWdNLuoAAAAASUVORK5CYII=", name: "outbound.png"}
1: {@odata.type: "#microsoft.graph.fileAttachment", contentBytes: "iVBORw0KGgoAAAANSUhEUgAAAQAA…eGOdrvC6af95tuTmRRrb4fxZWJvYuBoVJAAAAAElFTkSuQmCC", name: "inbound.png"}
`
这是我使用 api 发送邮件的方式
sendMail: async function(accessToken, email) {
const client = getAuthenticatedClient(accessToken);
const sentResult = await client.api('/users/{tenantid}/sendMail').post(email);
}
问题是,邮件已发送,但为什么没有附件
这就是我阅读文件的方式
var attachments = [];
function addAttachments() {
allFiles.forEach(a => {
let reader = new FileReader();
reader.readAsDataURL(a);
reader.onload = function() {
attachments.push({
'@odata.type': "#microsoft.graph.fileAttachment",
name: a.name,
contentType: a.type,
contentBytes: reader.result.split(',')[1],
});
};
})}
这里是邮件对象的控制台日志 email_object
这是我将对象字符串化后的结果
{"message":{"subject":"[AU1588259832480]-random subject","body":{"contentType":"HTML","content":"<p>body test</p>"},"toRecipients":[{"emailAddress":{"address":"email@test.com"}}],"internetMessageId":"AU1588259832480","attachments":[]}}
attachmen 对象是空的,但为什么?
您的 contentBytes
不应包含 data:image/png;base64
序言。那应该只是您图像中的 base64 编码字节。
"attachments": [
{
"@odata.type": "#microsoft.graph.fileAttachment",
"contentBytes": "/9j/4AAQSkZJRgABAQEAYAB...",
"name": "outbound.png",
"contentType": "image/png"
}
]
这是我解决空附件的方法,基于此post
im new posting on stack overflow
function getBase64(file, onLoadCallback) {
return new Promise(function(resolve, reject) {
var reader = new FileReader();
reader.onload = function() { resolve(reader.result); };
reader.onerror = reject;
reader.readAsDataURL(file);
});
}
const Send = {
message: {
subject: `[${quoteNumb ? quoteNumb : quoteHomeNumber}]-${Email.subject}`,
body: {
contentType: "HTML",
content: Email.body
},
toRecipients: [],
attachments: []
}};
async function sendMail() {
for (const a of allFiles) {
var fileData = await getBase64(a).catch(err => {
console.log(err)
});
Send.message.attachments.push({
'@odata.type': "#microsoft.graph.fileAttachment",
name: a.name,
contentType: a.type,
contentBytes: fileData.split(',')[1]
});
}
graph.sendMail(AccesToken, Send);}