简体   繁体   English

gmail和node.js的POP3对话

[英]POP3 conversation with gmail and node.js

I am trying to write a simple POP3 client for gmail in node.js. 我正在尝试在node.js中为gmail写一个简单的POP3客户端。 I initially wrote one where I enter the POP3 commands and they get sent to the server, and the server responds. 我最初写了一个我在其中输入POP3命令的命令,然后将它们发送到服务器,服务器响应。 This works fine because I can wait for the server's response before entering the next command. 因为我可以在输入下一个命令之前等待服务器的响应,所以它工作正常。

But I want to make an automated conversation, where the program itself sends the commands and awaits its own responses. 但是我想进行一次自动对话,程序本身在其中发送命令并等待自己的响应。 Simply doing: 简单地做:

stream.write('USER ***&@gmail.com');
stream.write('PASS *****');
stream.write('LIST');

doesn't work, because of the asynchronous nature of streams in node. 由于节点中流的异步特性而无法正常工作。 I have to wait for the stream's 'data' event before sending the next message, otherwise, nothing happens at all. 在发送下一条消息之前,我必须等待流的“数据”事件,否则,什么也不会发生。 So tried it like this: 所以这样尝试:

var tls =require ('tls');

var stream = tls.connect(995,'pop.gmail.com', function() {
    console.log ('Conexion establecida');
});

stream.on('data', function(data)
{
    var str = data.toString();
    console.log(str.substr(0,14));
    if(str.substr(0,14)=="+OK Gpop ready")
    {
        console.log('Sending username...');
        process.nextTick(function() {
            stream.write ('USER ***********@gmail.com');
        });
    }

    if(str.substr(0,14)=="+OK send PASS")
    {
        console.log('Recieving list of email...');
        process.nextTick(function(){
            stream.write('PASS *********');
        });
    }
});

The idea is that the 'data' event listener handles the sending of the next command depending on the last reply recieved. 这个想法是“数据”事件侦听器根据收到的最后一个答复来处理下一个命令的发送。 Again, doing stream.write alone did not seem to work, so I wrapped the write() calls in process.nextTick() calls. 同样,单独执行stream.write似乎无效,因此我将write()调用包装在process.nextTick()调用中。 It seems that the USER command is sent, but never the PASS command. 似乎已发送USER命令,但从未发送PASS命令。 Can someone tell me why? 有人可以告诉我为什么吗? Or if there is a more effective way to do this? 还是有更有效的方法来做到这一点? Thanks for any information. 感谢您提供任何信息。

Your commands are being sent, however they are not being recognized, as they are missing the newline character, which denotes the end of a command. 您的命令正在发送,但是由于缺少换行符(表示命令的末尾)而无法被识别。 Adding a newline character to the end of your commands should get it working. 在命令末尾添加换行符应该可以使其正常工作。

Additionally, you can simplify your commands by implementing a queue like so: 此外,您可以通过实现如下队列来简化命令:

var tls = require('tls');
var commands = [];

commands.push('USER ***********@gmail.com\n');
commands.push('PASS *********\n');

var stream = tls.connect(995, 'pop.gmail.com', function () {
    console.log('Conexion establecida');
});

stream.on('data', function (data) {
    if (data.toString().indexOf('OK') > -1 && commands.length) {
        stream.write(commands.shift());
    }
});

Without seeing all the code, it is a bit difficult to answer. 如果不看所有代码,很难回答。

However I noticed that in your first example, you write 但是我注意到在第一个示例中,您编写了

stream.write('USER * &@gmail.com'); stream.write('USER * &@gmail.com');

with an ampersand "&" 和符号“&”

However in the second example there is no ampersand - could that be the source of the issue? 但是,在第二个示例中,没有“&”号-是问题的根源吗?

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

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