我使用Microsoft Graph SDK毫无问题地发送电子邮件,但是当我添加附件时,该附件不会出现在发送的邮件中。 此处的附件类型为“斑点”。并为PDF格式。 我已经解决了几个与此问题相对应的stackoverflow问题,但没有一个解决了我的问题,这就是为什么我在这里问我的问题。
let graphMailAttachments = new Array<any>();
//attachments is an array of blob
attachments.forEach(async attachment => {
let file = await new Response(attachment).arrayBuffer();
graphMailAttachments.push({
"contentBytes": file,
"size": attachment.size,
"@odata.type" : "#microsoft.graph.fileAttachment",
"name": 'your-notes.pdf',
"id" : this.newGuid(),
})
});
subject = subject || "Your notes";
body = body || "Your notes";
let recipients = new Array<any>();
to.forEach(receiver => {
recipients.push({
emailAddress:{
address: receiver.email
}
})
});
let mail = {
message: {
subject: subject,
body: {
contentType: "Text",
content: body,
},
toRecipients: recipients,
attachments: graphMailAttachments,
},
SaveToSentItems : "true"
}
return this.graphClient.api('/me/sendMail').post(mail);
我做错什么了吗?附件只是没有出现在邮件中,有点奇怪。
答案 0 :(得分:1)
根据他们的样本,将内容类型设置为application / pdf,然后base64编码文件字节。您看到js示例代码部分了吗?https://docs.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=javascript#request-3
答案 1 :(得分:1)
在通过邮件发送附件之前,您需要将附件从arraybuffer转换为base64字符串。我使用了以下方法,对我来说效果很好:
arrayBufferToBase64( buffer ) : string {
var binary = '';
var bytes = new Uint8Array( buffer );
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode( bytes[ i ] );
}
return btoa( binary );
}
我从this stackoverflow答案中得到了它: