我正在尝试使用节点 imap 模块访问我的 gmail。
我尝试了邮件侦听器2的基本示例,如下所示。我用我的gmail地址,密码和 imap.gmail.com 替换为主机。但是,我面临以下错误消息。
{ [错误:请通过您的网络浏览器登录: https://support.google.com/mail/accounts/answer/78754(失败)] textCode: 'ALERT', source: 'authentication' } imap断开连接
我检查了我的gmail设置,为低安全性的应用程序启用了它。已启用 IMAP 并验证我的凭据是否正确。有人可以告诉我我错过了什么以及我哪里出错了。我尝试了其他node-imap模块,如npm-imap和imap-simple。
var MailListener = require("mail-listener2");
var mailListener = new MailListener({
username: "imap-username",
password: "imap-password",
host: "imap-host",
port: 993, // imap port
tls: true,
tlsOptions: { rejectUnauthorized: false },
mailbox: "INBOX", // mailbox to monitor
searchFilter: ["UNSEEN", "FLAGGED"], // the search filter being used after an IDLE notification has been retrieved
markSeen: true, // all fetched email willbe marked as seen and not fetched next time
fetchUnreadOnStart: true, // use it only if you want to get all unread email on lib start. Default is `false`,
mailParserOptions: {streamAttachments: true}, // options to be passed to mailParser lib.
attachments: true, // download attachments as they are encountered to the project directory
attachmentOptions: { directory: "attachments/" } // specify a download directory for attachments
});
mailListener.start(); // start listening
// stop listening
//mailListener.stop();
mailListener.on("server:connected", function(){
console.log("imapConnected");
});
mailListener.on("server:disconnected", function(){
console.log("imapDisconnected");
});
mailListener.on("error", function(err){
console.log(err);
});
mailListener.on("mail", function(mail, seqno, attributes){
// do something with mail object including attachments
console.log("emailParsed", mail);
// mail processing code goes here
});
mailListener.on("attachment", function(attachment){
console.log(attachment.path);
});
我遇到了同样的问题,作为快速修复,您可以启用对"安全性较低的应用程序"的访问,请转到此处:
https://support.google.com/accounts/answer/6010255
您也可以生成应用程序密码,但我认为您必须激活双因素身份验证(老实说,我不太确定)。希望对您有所帮助。