【问题标题】:How to get email contents using EWS/MAPI for nodejs如何使用 EWS/MAPI for nodejs 获取电子邮件内容
【发布时间】:2022-07-23 22:32:43
【问题描述】:

使用这个 node-ews 包,我可以发送电子邮件,但我还没有找到一个很好的例子来说明如何从收件箱文件夹中读取邮件并获取电子邮件的文本和附件。

我已阅读 Microsoft 文档,例如:https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-work-with-exchange-mailbox-items-by-using-ews-in-exchange#get-an-item-by-using-the-ews-managed-api,但示例使用 C#、C++ 或 VB。

但是,我想为此使用 Nodejs。

【问题讨论】:

    标签: javascript node.js exchangewebservices mapi


    【解决方案1】:

    **我找到了使用邮件解析器提取每个内容的最佳方法。 见下文

    // 首先阅读收件箱中的电子邮件

    const EWS = require('node-ews');
    const simpleParser = require('mailparser').simpleParser;
    // exchange server connection info
    const ewsConfig = {
      username: 'username',
      password: 'password',
      host: 'hostname'
    };
    const options = {
      rejectUnauthorized: false,
      strictSSL: false
    };
    // initialize node-ews
    const ews = new EWS(ewsConfig, options);
    var ewsFunction = 'FindItem';
        var ewsArgs = {
            'attributes': {
                'Traversal': 'Shallow'
            },
            'ItemShape': {
                't:BaseShape': 'Default'
            },
            'ParentFolderIds' : {
                'DistinguishedFolderId': {
                'attributes': {
                    'Id': 'inbox'
                }
              }
            }
        };
        // Itreate over all the emails and store Id and ChangeKey.
        ews.run(ewsFunction, ewsArgs, ewsSoapHeader)
        .then(result => {
            // Iterate over the result and extract Id and ChangeKey of the messages and pass those to GetItem function to read messages
        })
    

    // 用于读取 FindItem 返回的单个消息(使用 Id 和 ChangeKey)

    var ewsFunction = 'GetItem';
          var ewsArgs = {
            'ItemShape': {
              'BaseShape': 'Default',
              'AdditionalProperties': {
                'FieldURI': [
                  { 'attributes': { 'FieldURI': 'item:MimeContent'}}
                ]
              }
            },
            'ItemIds': {
              'ItemId': {
                'attributes': {
                  'Id': Id,
                  'ChangeKey': ChangeKey
                }
              }
            }
          };
          await ews.run(ewsFunction, ewsArgs, ewsSoapHeader)
          .then(result => {
              // Iterate over the result and extract meesage
              const {Message} = result.ResponseMessages.GetItemResponseMessage.Items
              let mimeContent = Buffer.from(Message.MimeContent['$value'], 'base64').toString('binary');// decode mime content
              simpleParser(mimeContent).then(async function (mail) {
                console.log("mail")
                console.log(mail.attachments)
                console.log(mail.headers.get('message-id'))
                console.log(mail.headers.get('references'))
                console.log(mail.headers.get('in-reply-to'))
                console.log({
                  // text: mail.text,
                  // html: mail.html ? mail.html.replace(/<meta([^>]+)>/g, "") : "",
                  from: (mail.from) ? mail.from.value.map(item => item.address) : [],
                  to: (mail.to) ? mail.to.value.map(item => item.address) : [],
                  cc: (mail.cc) ? mail.cc.value.map(item => item.address) : [],
                  bcc: (mail.bcc) ? mail.bcc.value.map(item => item.address) : [],
                  messageId: mail.messageId,
                  subject: mail.subject
                })
              }).catch((err) => {
                console.log("err")
                console.log(err)
              })
          })
    

    在这里,您将获得带有附件的完整解析邮件内容。快乐编码!!!

    【讨论】:

      猜你喜欢
      • 2011-07-11
      • 2020-07-04
      • 2015-04-05
      • 2020-08-17
      • 1970-01-01
      • 1970-01-01
      • 2019-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多