Search code examples
node.jsemailsmtpnodemailer

How to view SMTP credentials to send mail in node js


I am developing an application in node js, which has functionality of sending email. To do the task I need an SMTP username and password. But I can't find the place where I can get it. I am using the following code to do the operation,

var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport('smtps://user%40gmail.com:pass@smtp.gmail.com');
var mailOptions = {
    from: 'xxx@gmail.com', 
    to: 'yyy@gmail.com', 
    subject: 'Hello ✔', 
    text: 'Hello world 🐴', 
    html: '<b>Hello world 🐴</b>'
};

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

So can anybody help me to view SMTP credentials to send an email.

Thanks in advance


Solution

  • You just need to enter your credentials in this line of code

    var transporter = nodemailer.createTransport('smtps://user%40gmail.com:pass@smtp.gmail.com');

    Replace user with your email username and replace pass with your email password. Also, note if you have special character # in your password, then it will not work because it will break it into two pieces. I think will happen with other special characters like %,&,@ too. If you have these special characters in your password I recommend to use this structure:

    var transporter = nodemailer.createTransport( { host: "smtp.gmail.com", // hostname secureConnection: true, // use SSL port: 465, // port for secure SMTP auth: { user: "yourUserName@gmail.com", pass: "yourPassword" } });