最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论