繁体   English   中英

Microsoft Graph API outlook 发送附件

[英]Microsoft Graph API outlook send attachments

Microsoft graph outlook API 如何发送附件? 我了解发送附件直到内容字节的所有内容。 我需要发送的文件是一个word文档a 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('/');
    }
  }

我发现 param 将文件编码为 base64

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM