Akababa
Akababa

Reputation: 357

Nodemailer with gmail service account

I'm implementing a "no-reply" account for sending automated emails. Do I need the access key/refresh token, and if so where can I generate them for a service account?

At the moment I have this:

noreply = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 465,
    secure: true,
    auth: {
        type: 'OAuth2',
        user: smtpConfig.client_email,
        serviceClient: smtpConfig.client_id,
        privateKey: smtpConfig.private_key,
    }
});

Which unfortunately gives me this:

{ Error: Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8  https://support.google.com/mail/?p=BadCredentials h81sm3513839itb.18 - gsmtp
    at SMTPConnection._formatError
...

Thanks!

Upvotes: 0

Views: 1348

Answers (1)

ender
ender

Reputation: 311

you do not need a refresh token with a service account and nodemailer will generate the access key for you automatically

You cannot only use a service account to send emails for a GSuite account and not a gmail account.

If you have a gmail account you can use 3-legged OAuth2 authentication Or turn on 2FA, generate an App Password and use that as seen here

If you ARE using a GSuite account you can use the ServiceAccount but you will have to make sure it has G Suite Domain-wide Delegation as described here and then you need to give access to the GSuite Domain as described here

Upvotes: 1

Related Questions