用NodeJs简易实现Smtp客户端

本文介绍了如何使用Node.js创建一个简单的SMTP客户端。通过讲解关键步骤和代码示例,帮助读者理解SMTP通信过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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<nameAry.length;i++) {
var name = nameAry[i];
(function(name,isLast) {
self.addListener("from",function() {
if(isLast) {
self.operationing.push("lastrcpt");
} else {
self.operationing.push("notlastrcpt");
}
self.socket.write("RCPT TO:"+name+"\r\n");
});
})(name,i==nameAry.length-1);
}
},
from : function(addr) {
var self = this;
this.addListener("authed",function() {
self.operationing.push("from");
self.socket.write("MAIL FROM:"+addr+"\r\n");
});
},
handleEvent : function(eventName,func) {
if(eventName.indexOf("username")>=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<funcs.length);i++) {
funcs[i]();
}
}
}
},
data : function(data) {
var self = this;
this.addListener("rcptok",function() {
self.operationing.push("dataCommand");
self.socket.write("DATA\r\n");
self.addListener("startData",function() {
self.operationing.push("transferdata");
self.socket.write(data+"\r\n.\r\n");
});
});
}
}

调用时:

var mail = require("./mail");

var sc = new mail.SmtpClient("202.108.3.190");//host[,port,ifssl]

//var sc = new mail.SmtpClient("x.x.x.x",465,true);

sc.auth("xxxxx","xxxx");

sc.sendTo(["xxx@xxx","xxx@xxx","xxx@xxx"]);

sc.from("xxx@xxx");

sc.data("From: FuLi <perfectrobin@sina.com>\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<h1>This is a javascript implemented smtp client</h1>");

sc.send();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值