使用emailjs在nodejs中发送错误时收到错误

时间:2015-03-24 06:49:20

标签: email nodemailer

[error: can't set headers after they are sent.]
 createCredentials() is deprecated, use tls.createSecureContext instead
 {[Error: bad response on command '-']
 code:2
 smtp : '550 5.3.4 Requested action not token; To continue sending messages,
 please sign in to your account.\n}

我一直在尝试使用emailjs和nodemailer在nodejs中发送电子邮件,但我继续收到上述错误。

transportEmail: email.server.connect({
  user:    "ghConnectUs@outlook.com",
  password:"******",
  host:    "smtp-mail.outlook.com",
  tls: {ciphers: "SSLv3"}
})

注意:我已包含所有模块。

我希望有人能指出我正确的道路。我只想在节点应用程序中使用outlook或gmail发送给我。

1 个答案:

答案 0 :(得分:0)

以下是从nodejs app(通过Gmail)发送电子邮件的代码:

使用Nodemailer v1.3.4

var nodemailer = require("nodemailer");    
var transporter = nodemailer.createTransport({
    service: "Gmail",
    auth: {
        user: "email_id_of_gmail_account",
        pass: "password_of_gmail_account"
    }
});

var mailOptions = {
    from: 'sender_email_id', // sender address 
    to: 'receiver_email_id, some_other_email_if_requierd', // list of receivers 
    cc: 'cc_email_id'
    subject: 'subject text', // Subject line 
    text: 'body plain text', // plaintext body 
    html: '<b>body html</b>' // html body 
};

var sendEMail = function () {
    transporter.sendMail(mailOptions, function(error, info){
    if(error){
        console.log(error);
    }else{
        console.log('Message sent: ' + info.response);
    }
});
};

sendEmail();

注意:如果您使用的是Nodemailer 0.7或更低版​​本,那么将使用以下方式创建传输器对象:

var transporter = nodemailer.createTransport('SMTP', {
    service: "Gmail",
    auth: {
        user: "email_id_of_gmail_account",
        pass: "password_of_gmail_account"
    }
});

使用emailJs v0.3.16

var email = require("/node_modules/emailjs/email.js");
var server = email.server.connect({
    user: "emailId_of_gmail_account",
    password: "password_of_gmail_account",
    host: "smtp.gmail.com", 
    ssl: true // in case outlook, use "tls: {ciphers: "SSLv3"}"
});

var message = {
    text: "body text",
    from: "senderName <sender's_email_id>",
    to: "receiverName <receiver_email_id>",
    subject: "subject text",
    attachment:   // optional
        [
            {data: "<html>i <i>hope</i> this works! html </html>", alternative: true},
            {path: "path/to/file.zip", type:"application/zip", name:"renamed.zip"}
        ]
};

var sendEMail = function () {
    server.send(message, function (err, message) {
        console.log(err || message);
    });
};

sendEmail();