sending mail with nodemailer

2019-07-28 04:40发布

问题:

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.

回答1:

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



回答2:

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

Allow less secure apps to connect to Gmail.



回答3:

tls:{
        rejectUnauthorized:false
    }

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



回答4:

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