0

我使用 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 对象是空的,但为什么呢?

4

2 回答 2

0

你不应该有data:image/png;base64你的contentBytes. 那应该只是图像中的 base64 编码字节。

"attachments": [
    {
        "@odata.type": "#microsoft.graph.fileAttachment",
        "contentBytes": "/9j/4AAQSkZJRgABAQEAYAB...",
        "name": "outbound.png",
        "contentType": "image/png"
    }
]
于 2020-05-05T22:17:22.057 回答
0

这是我解决空附件的方法,基于这篇文章 如何将 FileReader base64 捕获为变量?

我在堆栈溢出上发布新帖子

  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);}
于 2020-05-07T22:36:56.843 回答