Microsoft图发送带有附件的电子邮件

时间:2020-04-24 23:10:06

标签: javascript reactjs microsoft-graph-api microsoft-graph-mail microsoft-graph-files

我正在使用Microsoft图形服务发送带有附件的电子邮件。 但是当我发送邮件时,这没有我设置的附件。 这是我生成的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对象为空,为什么?

2 个答案:

答案 0 :(得分:0)

data:image/png;base64的开头contentBytes不应该使用。那应该只是图像中base64编码的字节。

"attachments": [
    {
        "@odata.type": "#microsoft.graph.fileAttachment",
        "contentBytes": "/9j/4AAQSkZJRgABAQEAYAB...",
        "name": "outbound.png",
        "contentType": "image/png"
    }
]

答案 1 :(得分:0)

这是我根据这篇文章解决空附件的方法 How to capture FileReader base64 as variable?

im堆栈溢出时有新帖子

  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);}
相关问题