【问题标题】:How to get data of new mail message in node-imap如何在 node-imap 中获取新邮件消息的数据
【发布时间】:2022-05-10 20:59:18
【问题描述】:

我需要在新邮件消息事件中获取新邮件消息的信息。具体来说:

  • 发送邮件的电子邮件地址
  • 消息标题
  • 留言内容(文字)

我此时的代码:

let Imap = require('node-imap')

let imap = new Imap({
    user: '***@gmail.com',
    password: '***',
    host: 'imap.gmail.com',
    port: 993,
    tls: true
});
imap.on('ready', () => {
    imap.openBox('INBOX', true, (err, box) => {
        if (err) console.log(err)
        else console.log('Recipient is ready for accepting')
    })
})
imap.on('mail', mail => {
    const title = ?
    const subject = ?
    const email = ?
    console.log(`Email author: ${email} Title: ${title}, Description: ${subject}`)
})

imap.connect()

【问题讨论】:

    标签: javascript node.js imap


    【解决方案1】:

    imap.on('mail', ...) 没有提供新邮件的信息。只有一些新邮件。

    mail(numNewMsgs) - 新邮件到达时发出 当前打开的邮箱。

    要获取邮件数据,请使用imap.seq.fetchimap.search。 你可以在这里找到一些例子:https://github.com/mscdex/node-imap#connection-events

    也许这会有所帮助 (https://github.com/mscdex/node-imap/issues/359#issuecomment-37099842):

    imap.search([ 'NEW' ], function(err, results) {
       if (err || !results.length) return imap.end();
    
       // fetch all resulting messages
       imap.fetch(results, ....);
       // or just fetch only the first result... which may not necessarily be newest
       // because the server can return results in any order
       imap.fetch(results[0], ...);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      相关资源
      最近更新 更多