Error: Invalid login: Application-specific password required

21,755

Solution 1

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.

Solution 2

You need to use an application password for this purpose. This issue will arise when 2 Step verification is turned-on for your Gmail account. You can bypass it by using app password. here is how to generate an app password.

  1. Select your profile icon in the upper-right corner of Gmail, then select Manage Google Account.

  2. Select Security in the left sidebar.

  3. Select App passwords under the Signing into Google section. You're then asked to confirm your Gmail login credentials.

  4. Under Select app, choose Mail or Other (Custom name), then select a device.

  5. Select Generate.

  6. Your password appears in a new window. Follow the on-screen instructions to complete the process, then select Done.

google doc : https://support.google.com/mail/answer/185833?hl=en#zippy=%2Cwhy-you-may-need-an-app-password

Solution 3

Generate a password from https://security.google.com/settings/security/apppasswords and use that password instead.

Share:
21,755
SHUBHAM SHEDGE
Author by

SHUBHAM SHEDGE

Updated on July 09, 2022

Comments

  • SHUBHAM SHEDGE
    SHUBHAM SHEDGE almost 2 years

    i want send Welcome notification when user sign in using Cloud-Function with firebase auth so i m using nodejs CLI and run the code

    my index.js file 'use strict';

        const functions = require('firebase-functions');
        const nodemailer = require('nodemailer');
        // Configure the email transport using the default SMTP transport and a GMail account.
        // For Gmail, enable these:
        // 1. https://www.google.com/settings/security/lesssecureapps
        // 2. https://accounts.google.com/DisplayUnlockCaptcha
        // For other types of transports such as Sendgrid see https://nodemailer.com/transports/
        // TODO: Configure the `gmail.email` and `gmail.password` Google Cloud environment variables.
    
        const gmailEmail = functions.config().gmail.email;
        const gmailPassword = functions.config().gmail.password;
        const mailTransport = nodemailer.createTransport({
          service: 'gmail',
          auth: {
            user: gmailEmail,
            pass: gmailPassword,
          },
        });
    
        // Your company name to include in the emails
        // TODO: Change this to your app or company name to customize the email sent.
        const APP_NAME = 'Cloud Storage for Firebase quickstart';
    
        // [START sendWelcomeEmail]
        /**
         * Sends a welcome email to new user.
         */
        // [START onCreateTrigger]
        exports.sendWelcomeEmail = functions.auth.user().onCreate((user) => {
        // [END onCreateTrigger]
          // [START eventAttributes]
          const email = user.email; // The email of the user.
          const displayName = user.displayName; // The display name of the user.
          // [END eventAttributes]
    
          return sendWelcomeEmail(email, displayName);
        });
        // [END sendWelcomeEmail]
    
        // [START sendByeEmail]
        /**
         * Send an account deleted email confirmation to users who delete their accounts.
         */
        // [START onDeleteTrigger]
        exports.sendByeEmail = functions.auth.user().onDelete((user) => {
        // [END onDeleteTrigger]
          const email = user.email;
          const displayName = user.displayName;
    
          return sendGoodbyeEmail(email, displayName);
        });
        // [END sendByeEmail]
    
        // Sends a welcome email to the given user.
        async function sendWelcomeEmail(email, displayName) {
          const mailOptions = {
            from: `${APP_NAME} <[email protected]>`,
            to: email,
          };
    
          // The user subscribed to the newsletter.
          mailOptions.subject = `Welcome to ${APP_NAME}!`;
          mailOptions.text = `Hey ${displayName || ''}! Welcome to ${APP_NAME}. I hope you will enjoy our service.`;
          await mailTransport.sendMail(mailOptions);
          console.log('New welcome email sent to:', email);
          return null;
        }
    
        // Sends a goodbye email to the given user.
        async function sendGoodbyeEmail(email, displayName) {
          const mailOptions = {
            from: `${APP_NAME} <[email protected]>`,
            to: email,
          };
    
          // The user unsubscribed to the newsletter.
          mailOptions.subject = `Bye!`;
          mailOptions.text = `Hey ${displayName || ''}!, We confirm that we have deleted your ${APP_NAME} account.`;
          await mailTransport.sendMail(mailOptions);
          console.log('Account deletion confirmation email sent to:', email);
          return null;
        }
    

    i refer this code https://github.com/firebase/functions-samples/blob/master/quickstarts/email-users/functions/index.js

    but after i ran the code i got error

        Error: Invalid login: 534-5.7.9 Application-specific password required. Learn more at
    534 5.7.9  https://support.google.com/mail/?p=InvalidSecondFactor i82sm13686303ilf.32 - gsmtp
        at SMTPConnection._formatError (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:784:19)
        at SMTPConnection._actionAUTHComplete (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:1523:34)
        at SMTPConnection._responseActions.push.str (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:550:26)
        at SMTPConnection._processResponse (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:942:20)
        at SMTPConnection._onData (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:749:14)
        at TLSSocket.SMTPConnection._onSocketData.chunk (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:195:44)
        at emitOne (events.js:116:13)
        at TLSSocket.emit (events.js:211:7)
        at addChunk (_stream_readable.js:263:12)
        at readableAddChunk (_stream_readable.js:250:11)
    

    i also Allow less secure apps From your Google Account and also done 2 step-verification Here but still got an error

    I read all "Similar Questions" here in stackoverflow and I don't know if I need anything else or if I'm doing anything bad

    • Renaud Tarnec
      Renaud Tarnec about 4 years
      Did you "Set the gmail.email and gmail.password Google Cloud environment variables to match the email and password of the Gmail account used to send emails" as explained at point #8 here? Also, have you read this page?
    • SHUBHAM SHEDGE
      SHUBHAM SHEDGE about 4 years
      yes i did @ Renaud Tarnec
  • Manuel Rivera
    Manuel Rivera over 3 years
    Just to say it worked for me. As I have '2-factor authentication' active in my gmail account, I had to go to App Passwords, in my account config, and allow Google to generate a 16 chars password for me. I then used this password in my application and everything worked just fine ...
  • Irfan Khan
    Irfan Khan about 2 years
    thanks bro you saved my precious time
  • Admin
    Admin about 2 years
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
  • Ronak Shah
    Ronak Shah about 2 years
    Add an app password from here: myaccount.google.com/apppasswords