用NodeJs简易实现Smtp客户端


用 NodeJs 简易实现 Smtp 客户端 var net = require('net'); var SmtpClient = exports.SmtpClient =function(host,port,useSecure){ this.host = host; this.port = port||25; this.socket = new net.Stream(); var self = this; this.operationing = []; this.socket.addListener('data',function(data){ var dataStr = data.toString(); console.log(dataStr); var msg = !/([^ ]+)$/.test(dataStr)||RegExp.$1; var code = (dataStr.match(/^\d+/g)||[""])[0]; switch(code) { case "250": var operation = self.operationing.pop(); if(operation == "connect") { self.connected = true; self.handleEvent("connect"); } else if(operation == "from") { self.handleEvent("from"); } else if(operation == "lastrcpt") { self.handleEvent("rcptok"); } else if(operation == 'transferdata') { self.handleEvent("exit"); } break; case "334": var operation = self.operationing.pop(); if(operation == "dataCommand") { self.handleEvent("startData"); } else { self.operationing.push(operation); } var key = new Buffer(msg,"base64").toString().toLowerCase(); self.handleEvent(key); break; case "235" : self.authed = true; self.handleEvent("authed"); break; case "221" : self.socket.end(); break; } }); if(useSecure) { this.socket.setSecure(); } this.listeners = {}; } SmtpClient.prototype = { send : function() { this.socket.connect(this.port,this.host); var self = this; this.socket.addListener('connect',function() { self.operationing.push("connect"); self.socket.write("HELO\r\n"); }); }, auth : function(username,password) { var needAuth = !!username; this.options = { username:username, password:password }; if(needAuth) { var self = this; this.addListener("connect",function() { if(!self.authed) { self.socket.write("AUTH LOGIN\r\n"); } }); } else { this.authed = true; this.handleEvent('authed'); } }, addListener : function(eventName,listenerFunc) { var ary = this.listeners[eventName]?this.listeners[eventName]: (this.listeners[eventName]=[]); ary.push(listenerFunc); this.handleEvent(eventName,listenerFunc) }, sendTo : function(nameAry) { var self = this; for(var i=0;i=0) { this.socket.write(new Buffer(this.options.username).toString("base64")+"\r\n"); } if(eventName.indexOf("password")>=0) { this.socket.write(new Buffer(this.options.password).toString("base64")+"\r\n"); } if(eventName == "exit") { this.socket.write("QUIT\r\n"); } if((eventName == "connect"|| eventName == "authed"|| eventName == "from"|| eventName == "rcptok"|| eventName =="startData")&&this.connected) { if(func) { func(); } else { var funcs = this.listeners[eventName]; for(var i=0;funcs&&(i\r\n"+ "Subject: Smtp Client implementation\r\n"+ "To: xxx@xxx.xx\r\n"+ "Content-type: text/html\r\n"+ "Content-transfer-encoding: 8BIT\r\n

This is a javascript implemented smtp client

"); sc.send();
还剩2页未读

继续阅读

下载pdf到电脑,查找使用更方便

pdf的实际排版效果,会与网站的显示效果略有不同!!

需要 3 金币 [ 分享pdf获得金币 ] 0 人已下载

下载pdf

pdf贡献者

huang4315

贡献于2012-08-27

下载需要 3 金币 [金币充值 ]
亲,您也可以通过 分享原创pdf 来获得金币奖励!
下载pdf