Error sending email: Error: Invalid login: 535 5.7.3 Authentication unsuccessful [PN2PR01CA0076.INDPRD01.PROD.OUTLOOK.COM 2023-09-22T12:34:03.188Z 08DBBAC9AE78B1D7]
Where do i am doing wrong ?
// Define the function for sending SMTP email with OAuth 2.0 authentication
const sendEmailWithOAuth2 = async (req, res) =>{
// Set up Microsoft Identity Platform (MSAL) client
const msalConfig = {
auth: {
clientId: '591a9a4f-3fb0-4f12-987e-ea6120e747f0',
authority: 'https://login.microsoftonline.com/76151ff0-c1a1-441c-8aeb-ef2333d01ed7',
clientSecret: 'XXXXXXXXXXXXXXXXXINa8Q~Qc0X~',
},
};
const cca = new ConfidentialClientApplication(msalConfig);
// Define the scopes required for sending emails via Outlook
const scopes = ['https://outlook.office365.com/.default'];
// Acquire a token
let accessToken;
try {
const tokenResponse = await cca.acquireTokenByClientCredential({
scopes,
});
accessToken = tokenResponse.accessToken;
console.log("accessToken-----------------");
console.log(accessToken);
} catch (error) {
console.error('Error acquiring token:', error);
return;
}
// Set up the transport
const transport = nodemailer.createTransport({
host: 'smtp.office365.com',
port: 587, // TLS
secure: false, // Upgrade to TLS
auth: {
type: 'OAuth2',
user: 'support.p2e@yuwaah.org',
clientId: '-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0e747f0',
accessToken
pass: 'qncgdbqywcjbypsn',
},
tls: {
ciphers: 'SSLv3', // Specify the desired SSL/TLS version (e.g., SSLv3)
},
});
// Send an email
const mailOptions = {
from: 'support.p2e@yuwaah.org',
to: 'payertrust@gmail.com',
subject: 'Test Email',
text: 'This is a test email sent from Node.js using Outlook SMTP.',
};
try {
const info = await transport.sendMail(mailOptions);
console.log('Email sent:', info.response);
res.send(info.response);
} catch (error) {
console.error('Error sending email:', error);
}
}
Where do i am doing wrong ?