问题
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:
This is due to your SMTP security for unauthorised user so for overcome this problem you need set rejectUnauthorized:false
Hope this will helpyou.
回答3:
If you are using Gmail to send mails then try this.
Allow less secure apps to connect to Gmail.
回答4:
tls:{
rejectUnauthorized:false
}
fixed it by using the above code in mailer variable. Thanks guys
回答5:
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 👥" <recipe@example.com>', // sender address
to: 'bernard@example.com', // list of receivers
subject: 'Hello', // Subject line
text: 'Bonjour!' // plaintext body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
If is still doesn't work you have a little tuto here
回答6:
var mailer=nodemailer.createTransport({
host: 'mail.test.com',
port: 465,
secure: true, // secure:true for port 465, secure:false for port 587
transportMethod: 'SMTP',
auth: {
user: 'mymail@test.com',
pass: '1234'
}
});
The value for secure
should be true for port 465
and use transportMethod: 'SMTP'
来源:https://stackoverflow.com/questions/44885668/sending-mail-with-nodemailer