Username and Password not accepted when using nodemailer?

后端 未结 8 1715
悲哀的现实
悲哀的现实 2020-12-03 04:44

This is my settingController:

var sendSmtpMail = function (req,res) {
  var transport = nodemailer.createTransport({
  service:\'gmail\',
   auth: {
                 


        
相关标签:
8条回答
  • 2020-12-03 04:52

    Make sure you are using the right port: ie port: 587 as of now.

    Visit this link to allow Less secure App access: https://myaccount.google.com/lesssecureapps

    Enable the recaptcha at this link: https://accounts.google.com/b/0/DisplayUnlockCaptcha

    Wait for a few minutes and try to send an email.

    Alternatively use sendgrid. It has a free version.

    If you are not seeing the emails in your inbox, check the Spam folder.

    0 讨论(0)
  • 2020-12-03 04:53

    I think that first you need to Allow less secure apps to access account setting in your google account - by default this settings is off and you simply turn it on. Also you need to make sure that 2 factor authentication for the account is disabled. You can check how to disable it here.

    Then I use the following script to send emails from a gmail account, also tested with yahoo and hotmail accounts.

    const nodemailer = require('nodemailer');
    
    let transporter = nodemailer.createTransport({
        host: 'smtp.gmail.com',
        port: 587,
        secure: false,
        requireTLS: true,
        auth: {
            user: 'your.gmail.account@gmail.com',
            pass: 'your.password'
        }
    });
    
    let mailOptions = {
        from: 'your.gmail.account@gmail.com',
        to: 'receivers.email@domain.com',
        subject: 'Test',
        text: 'Hello World!'
    };
    
    transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
            return console.log(error.message);
        }
        console.log('success');
    });
    

    If you put the previous code in send-email.js for example, open terminal and write:

    node send-email
    

    You should see in the console - success, if the email was send successfully or the error message returned by nodemailer

    Don't forget to first do the setting - Allow less secure apps to access account.

    I hope this code will be useful for you. Good Luck!

    0 讨论(0)
  • 2020-12-03 04:57

    There are some account settings that are necessary for you to send through it using SMTP.

    If you have two-step verification enabled on the account, you will need to use an application specific password (created in the Gmail account) in the device settings: Signing in using application-specific passwords

    If not, please try this: sign into the Gmail account using a web browser at https://mail.google.com, then go to Settings > Accounts and Import > Other Google Account settings. Under Security, scroll down and enable access for less secure apps. This setting is required to enable SMTP, POP or IMAP access.

    If there is still a problem, try clearing Captcha: visit https://accounts.google.com/DisplayUnlockCaptcha and sign in with the Gmail username and password. If necessary (it's usually not), enter the letters in the distorted picture then press Continue. This will allow ten minutes for the device to register as an approved connection. Note that you must use the account you are trying to add to the device - if the browser is already signed into another account, you must sign out first. Also, you must trigger the device to make a connection within ten minutes of pressing Continue.

    Explanation from Google Support team

    0 讨论(0)
  • 2020-12-03 04:59

    If you have enabled 2-factor authentication on your Google account you can't use your regular password to access Gmail programmatically. You need to generate an app-specific password and use that in place of your actual password.

    Steps:

    • Log in to your Google account
    • Go to My Account > Sign-in & Security > App Passwords
    • (Sign in again to confirm it's you)
    • Scroll down to Select App (in the Password & sign-in method box) and choose Other (custom name)
    • Give this app password a name, e.g. "nodemailer"
    • Choose Generate
    • Copy the long generated password and paste it into your Node.js script instead of your actual Gmail password. (You don't need the spaces.)

    Your script will now look like this:

    var transporter = nodemailer.createTransport({
      service: 'gmail',
      auth: {
        user: 'YOUR-GMAIL-USERNAME@gmail.com',
        pass: 'YOUR-GENERATED-APP-PASSWORD'
      }
    });
    

    I hope this helps someone.

    0 讨论(0)
  • 2020-12-03 05:04

    You are using gmail service so your mail must be in gmail:

    var transport = nodemailer.createTransport({
    service:'gmail',
     auth: {
                 user: "asdfqweerrccb@gmail.com",
                 pass: "qwerr@wee"
            }
        });
    var mailOptions = {
            from: "transactions@gmail.com", 
            to:'umaraja1124@gmail.com', 
            subject: req.body.subject+"nodejs working ?", 
            text: "Hello world ?",  
        }
    transport.sendMail(mailOptions, function(error, response){
        if(error){
             res.send("Email could not sent due to error: "+error);
             console.log('Error');
        }else{
             res.send("Email has been sent successfully");
             console.log('mail sent');
        } 
    }); 
    
    0 讨论(0)
  • 2020-12-03 05:06

    https://myaccount.google.com/lesssecureapps if it is OFF, turn it ON to enable lesssecureapps. (i had same issue turn it on resolve my issue)

    0 讨论(0)
提交回复
热议问题