sending mail with nodemailer

2019-07-28 04:51发布

whenever i run the code I'm getting the error " Error: Greeting never received at SMTPConnection._formatError...."

function automatedMailSender(req,res){

var mailOptions = {
    from: "abc <abc@test.com>", // sender address
    to: 'nit@test.com', // list of receivers
    subject: 'Hello ', // Subject line
    text: 'Hello world ?', // plain text body
    html: '<b>Hello world ?</b>' // html body
 };


var mailer=nodemailer.createTransport({
    host: 'mail.test.com',
    port: 465,
    secure: false, // secure:true for port 465, secure:false for port 587
    auth: {
        user: 'mymail@test.com',
        pass: '1234'
    }
});

mailer.sendMail(mailOptions, (error, response)=>{
    if(error){
        console.log('mail not sent \n',error);
    }
    else{
        console.log("Message sent: " ,response.message);
    }   
 });
}

I'm I doing something wrong in the code.

4条回答
做自己的国王
2楼-- · 2019-07-28 05:04

If you are using Gmail to send mails then try this.

Allow less secure apps to connect to Gmail.

查看更多
老娘就宠你
3楼-- · 2019-07-28 05:04

Try something like that, it work perfectly for me:

//Create reusable transporter object using the default SMTP transport 
var transporter = nodemailer.createTransport(
'smtps://USERNAME%40gmail.com:PASSWORD@smtp.gmail.com');

// setup e-mail data 
var mailOptions = {
        from: '"Michel                                                                     
查看更多
Explosion°爆炸
4楼-- · 2019-07-28 05:09

The answer in right in your code

port: 465,
secure: false, // secure:true for port 465, secure:false for port 587

The value for "secure" should be true for port 465

查看更多
叛逆
5楼-- · 2019-07-28 05:14
tls:{
        rejectUnauthorized:false
    }

fixed it by using the above code in mailer variable. Thanks guys

查看更多
登录 后发表回答