nodejs smtp 发送电子邮件

2012-05-24 16:17  3583人阅读  评论 (0)

php发送邮件要等大概两秒时间,用户体验不好,没办法亮剑吧,nodejs出鞘,哈哈。

这只是一个简单的代码,只是简单原理,如果有需要还要自行加工。(写的弱爆了,不要拍砖哦。)

var net = require('net');

var host = 'smtp.test.com';
var port = 25;
var username = 'service@test.com';
var password = '123456';
var username_b64 = new Buffer(username).toString('base64');
var password_b64 = new Buffer(password).toString('base64');

var form_name = 'service';
var form_mail = 'service@test.com';
var form = '=?UTF-8?B?'+(new Buffer(form_name).toString('base64'))+'?= <'+form_mail+'>';

var to_name = 'user';
var to_mail = 'user@test.com';
var to = '=?UTF-8?B?'+(new Buffer(to_name).toString('base64'))+'?= <'+to_mail+'>';

var client = net.connect(25, 'smtp.test.com', function() {
    console.log('ehlo smtp.test.com');
    client.write('ehlo smtp.test.com\r\n');
    setTimeout(function(){
        console.log('auth login');
        client.write('auth login\r\n');
        setTimeout(function(){
            console.log(username_b64+'');
            client.write(username_b64+'\r\n');
            setTimeout(function(){
                console.log(password_b64+'');
                client.write(password_b64+'\r\n');
                setTimeout(function(){
                    console.log('mail from: <'+form_mail+'>');
                    client.write('mail from: <'+form_mail+'>\r\n'); // 发件人,真实的邮箱
                    setTimeout(function(){
                        console.log('rcpt to: <'+to_mail+'>');
                        client.write('rcpt to: <'+to_mail+'>\r\n'); // 收件人,真实的邮箱
                        setTimeout(function(){
                            console.log('data');
                            client.write('data\r\n');
                            setTimeout(function(){
                                console.log('from: '+form);
                                client.write('from: '+form+'\r\n');
                                console.log('to: '+to);
                                client.write('to: '+to+'\r\n');
                                console.log('subject: nodejs smtp mail test');
                                client.write('subject: nodejs smtp mail test\r\n');
                                console.log('MIME-Version: 1.0');
                                client.write('MIME-Version: 1.0\r\n');
                                console.log('Content-Type: text/plain; charset=UTF-8');
                                client.write('Content-Type: text/plain; charset=UTF-8\r\n\r\n');
                                console.log('test!!!\r\n');
                                client.write('test!!!\r\n\r\n');
                                console.log('.');
                                client.write('.\r\n');
                                setTimeout(function(){
                                    console.log('quit');
                                    client.write('quit\r\n');
                                }, 200);
                            }, 200);
                        }, 200);
                    }, 200);
                }, 200);
            }, 200);
        }, 200);
    }, 200);
});
client.on('data', function(data) {
    console.log(data.toString());
});
client.on('end', function() {
    console.log('client disconnected');
});





豫ICP备09035262号-1