Microsoft Graph API Outlook发送附件

时间:2020-02-24 15:38:57

标签: node.js api outlook microsoft-graph-api

如何使用Microsoft graph Outlook API发送附件?我了解所有发送附件的内容,直到内容字节为止。我需要发送的文件是Word文档,pdf和jpeg,是否要求我将文件转换为字节,如果可以,我该怎么做?我正在使用node.js,并且有以下代码:

  exports.send_mail = async function(req, res, next) {
    let parms = { title: 'Inbox', active: { inbox: true } };
    const accessToken = await authHelper.getAccessToken(req.cookies, res);
    const userName = req.cookies.graph_user_name;

    if (accessToken && userName) {
      parms.user = userName;

      // Initialize Graph client
      const client = graph.Client.init({
        authProvider: (done) => {
          done(null, accessToken);
        }
      });


      //read test.html
      //so you have to wait for the file to read then send it
      message = fs.readFileSync('views/test.html', 'utf8');

      console.log(rawImage.data.toString('utf8'))

      try {
        mailMess ={
          message:{
            subject: 'This is a test',
            body:{
              ContentType: 'HTML',
              Content: message
            },
            ToRecipients:[
              {
                EmailAddress:{
                  Address: 'name@email.com'
                }
              }
            ],
            "attachments": [
              {
                "@odata.type": "#microsoft.graph.fileAttachment",
                "name": "attachment.jpeg",
                "contentBytes": "not sure what to put here"
              }
            ]
          }
        }
        //sendmail
        const result = await client
        .api('/me/sendmail')
        .version('v1.0')
        .post(mailMess);
        res.status('202')

        parms.messages = result.value;
        res.redirect('/');
      } catch (err) {
        parms.message = 'Error retrieving messages';
        parms.error = { status: `${err.code}: ${err.message}` };
        parms.debug = JSON.stringify(err.body, null, 2);
        res.render('error', parms);
      }

    } else {
      // Redirect to home
      res.redirect('/');
    }
  }

1 个答案:

答案 0 :(得分:0)

我发现param将编码为base64的文件