Sending email via Node.js using nodemailer is not working

后端 未结 9 1843
悲&欢浪女
悲&欢浪女 2020-11-29 20:15

I\'ve set up a basic NodeJS server (using the nodemailer module) locally (http://localhost:8080) just so that I can test whether the server can actually send ou

9条回答
  •  盖世英雄少女心
    2020-11-29 20:52

    And install smtp module as dependency:

    npm install smtp

    var nodemailer = require('nodemailer');
    
    var transporter = nodemailer.createTransport({
      service: 'gmail',
      type: "SMTP",
      host: "smtp.gmail.com",
      secure: true,
      auth: {
        user: 'writeYourGmailId@gmail.com',
        pass: 'YourGmailPassword'
      }
    });
    
    var mailOptions = {
      from: 'xyz.khan704@gmail.com',
      to: 'azran.khan704@gmail.com',
      subject: 'Sending Email to test Node.js nodemailer',
      text: 'That was easy to test!'
    };
    
    transporter.sendMail(mailOptions, function(error, info){
      if (error) {
        console.log(error);
      } else {
        console.log('Email sent');
      }
    });
    

    Go to https://myaccount.google.com/lesssecureapps and change it ON because Some apps and devices use less secure sign-in technology, which makes your account more vulnerable. You can turn off access for these apps, which we recommend, or turn on access if you want to use them despite the risks.

提交回复
热议问题