Sending email via Node.js using nodemailer is not working

后端 未结 9 1845
悲&欢浪女
悲&欢浪女 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 21:18

    The answer is in the message from google.

    • Go to : https://www.google.com/settings/security/lesssecureapps

    • set the Access for less secure apps setting to Enable

    For the second part of the problem, and in response to

    I'm actually simply following the steps from the nodemailer github page so there are no errors in my code

    I will refer you to the nodemailer github page, and this piece of code :

    var transporter = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
        user: 'gmail.user@gmail.com',
        pass: 'userpass'
    }
    });
    

    It differs slightly from your code, in the fact that you have : nodemailer.createTransport("SMTP". Remove the SMTP parameter and it works (just tested). Also, why encapsulating it in a http server? the following works :

    var nodemailer = require('nodemailer');
    var transporter = nodemailer.createTransport({
        service: 'Gmail',
        auth: {
            user: 'xxx',
            pass: 'xxx'
        }
    });
    
    console.log('created');
    transporter.sendMail({
    from: 'xxx@gmail.com',
      to: 'xxx@gmail.com',
      subject: 'hello world!',
      text: 'hello world!'
    });
    

提交回复
热议问题