node.js - Sending emails using Nodemailer throgh Entra outlook domain with Oauth2 after receiving a 536 5.7.8 another step is re

Im making an Node.js app for my work that should send pdfs in emails through our Entra outlook work dom

Im making an Node.js app for my work that should send pdfs in emails through our Entra outlook work domain. Currently I am trying to do so with Oauth2 sonce we use MFA for our accounts and the farthest I have gotten is now receiving Error 536 5.7.8 another step is required in authentication.

What could be the source of this error and how would I bypass it?

So the app worked perfectly when sending emails from my gmail test account with an app password. When I moved to our domain I tried first with normal password which ended up in 536 5.7.8 authentication failure. Same happened when we in out Entra allowed app passwords and tried it with those. I have checked multiple times and tried with several different app passwords out of which none worked even days after generating them. App code

//App password code
const transporter = nodemailer.createTransport({
        host: "entradomain",
        port: 587,
        secure: false,
        auth: {
            user: "[email protected]", // req.body.sender
            pass: "app password" // req.body.pass
        }
    });
//App password server output
[2024-11-18 15:14:13] DEBUG [r7nmlU27tI] S: 250-PIPELINING
[2024-11-18 15:14:13] DEBUG [r7nmlU27tI] S: 250-SIZE 18874368
[2024-11-18 15:14:13] DEBUG [r7nmlU27tI] S: 250-VRFY
[2024-11-18 15:14:13] DEBUG [r7nmlU27tI] S: 250-ETRN
[2024-11-18 15:14:13] DEBUG [r7nmlU27tI] S: 250-AUTH DIGEST-MD5 PLAIN LOGIN
[2024-11-18 15:14:13] DEBUG [r7nmlU27tI] S: 250-ENHANCEDSTATUSCODES
[2024-11-18 15:14:13] DEBUG [r7nmlU27tI] S: 250-8BITMIME
[2024-11-18 15:14:13] DEBUG [r7nmlU27tI] S: 250-DSN
[2024-11-18 15:14:13] DEBUG [r7nmlU27tI] S: 250-SMTPUTF8
[2024-11-18 15:14:13] DEBUG [r7nmlU27tI] S: 250 CHUNKING
[2024-11-18 15:14:13] DEBUG [r7nmlU27tI] SMTP handshake finished
[2024-11-18 15:14:13] DEBUG [r7nmlU27tI] C: AUTH PLAIN 
[2024-11-18 15:14:13] DEBUG [mGNxKhx1V0M] S: 535 5.7.8 Error: authentication failed: authentication failure

So I caved and tried to set up Oath2. I registered the app in our Entra using microsoft Graph and msal library for it. That got me a new error that i have been stuck on for the past few days and can no longer find a way around.

//Oauth2 code
const accessToken = await getAccessToken();

const transporter = nodemailer.createTransport({
        host: "entradomain",
        port: 587,
        secure: false,
        auth: {
            type: 'OAuth2',
            user: "[email protected]", // req.body.sender
            clientId: "client id",
            clientSecret: 'client seecret',
            accessToken: accessToken,
        },
        tls: {
            // do not fail on invalid certs
            rejectUnauthorized: false
        },
        logger: true,
        debug: true,
    });

bellow is my msal config script

//msal config
const msal = require('@azure/msal-node');

const msalConfig = {
    auth: {
        clientId: 'clientid',
        authority:' id',
        clientSecret: 'secret',
    }
};

const tokenRequest = {
    scopes: ['/.default'],
    //scopes: ['/.default'],
    };

const pca = new msal.ConfidentialClientApplication(msalConfig);

module.exports = { pca, tokenRequest};

bellow is my token getting script

//get token script

//this script retrieves the Refresh token from the mcalConfig.js script

const { pca, tokenRequest } = require('./msalConfig.cjs');

async function getAccessToken() {
    try{
        const response = await pca.acquireTokenByClientCredential(tokenRequest);
        return response.accessToken;
    } catch (error) {
        console.error('Errorr getting the token is: ', error);
        throw error;
    }
}

module.exports = getAccessToken;

lastly here is the server response when I attempt to send an email using the Oauth2

[2024-11-18 13:57:27] DEBUG [5zsQd8LUlmw] S: 250-PIPELINING
[2024-11-18 13:57:27] DEBUG [5zsQd8LUlmw] S: 250-SIZE 18874368
[2024-11-18 13:57:27] DEBUG [5zsQd8LUlmw] S: 250-VRFY
[2024-11-18 13:57:27] DEBUG [5zsQd8LUlmw] S: 250-ETRN
[2024-11-18 13:57:27] DEBUG [5zsQd8LUlmw] S: 250-AUTH DIGEST-MD5 PLAIN LOGIN
[2024-11-18 13:57:27] DEBUG [5zsQd8LUlmw] S: 250-ENHANCEDSTATUSCODES
[2024-11-18 13:57:27] DEBUG [5zsQd8LUlmw] S: 250-8BITMIME
[2024-11-18 13:57:27] DEBUG [5zsQd8LUlmw] S: 250-DSN
[2024-11-18 13:57:27] DEBUG [5zsQd8LUlmw] S: 250-SMTPUTF8
[2024-11-18 13:57:27] DEBUG [5zsQd8LUlmw] S: 250 CHUNKING
[2024-11-18 13:57:27] DEBUG [5zsQd8LUlmw] SMTP handshake finished
[2024-11-18 13:57:27] DEBUG [5zsQd8LUlmw] C: AUTH XOAUTH2 

[2024-11-18 13:57:27] ERROR Send Error: Invalid login: 535 5.7.8 Error: authentication failed: another step is needed in authentication

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745573723a4633837.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信