node.js发送邮件email

首先我们还是得安装node的第三方用户发送email的依赖module 。

npm install nodemailer

安装好之后,还有一个问题需要注意,我们应该确保用来发送邮件的邮箱地址是打开只是IMAP 、SMTP功能的,这样才可以发送邮件成功。

//发送邮件
mail.SMTP = {
    use_authentication: true, //如果我们使用QQ等邮箱,这个必须写且为true
    host: 'smtp.qq.com', //定义用来发送邮件的邮箱服务器,一般是QQ这些的
    port: 25, //定义服务器端口,一般是25   ,如果是使用SSL端口一般为465,或者587
    ssl: false, //默认不适用SSL,可以省略不写
    user: '[email protected]', //邮箱用户名
    pass: '*****'   //输入邮箱密码
}
mail.send_mail(
        {
            sender: '[email protected]', //发送邮件的地址
            to: '[email protected]', //发给谁
            subject: '邮件标题', //主题
            body: '内容部分'
        },
        //回调函数,用户判断发送是否成功,如果失败,输出失败原因。
        function(error, success) {
            if (!error) {
                console.log('message success');
            } else {
                console.log('failed' + error);
            }
        }
); 

转载自:http://www.9958.pw/post/nodemailer

猜你喜欢

转载自ldl-xz.iteye.com/blog/2280711