Forum Discussion
milos95
Sep 05, 2023Copper Contributor
How to send emails thru nodemailer (node.js) from outlook? SMTP Disabled
Hello, I'm writing a small backend node.js application from which I'd like to send emails. I set everything up in the following fashion: try {
const transporter = nodemailer.createTran...
malik786
Jul 20, 2024Copper Contributor
const nodemailer = require('nodemailer');
const { ConfidentialClientApplication } = require('@azure/msal-node');
const config = {
auth: {
clientId: 'YOUR_CLIENT_ID',
authority: 'https://login.microsoftonline.com/YOUR_TENANT_ID',
clientSecret: 'YOUR_CLIENT_SECRET',
}
};
const cca = new ConfidentialClientApplication(config);
const getAccessToken = async () => {
const result = await cca.acquireTokenByClientCredential({
scopes: ['https://graph.microsoft.com/.default'],
});
return result.accessToken;
};
const sendEmail = async () => {
const accessToken = await getAccessToken();
const transporter = nodemailer.createTransport({
service: 'Outlook365',
auth: {
type: 'OAuth2',
user: 'email address removed for privacy reasons',
accessToken: accessToken,
}
});
const mailOptions = {
from: 'email address removed for privacy reasons',
to: 'email address removed for privacy reasons',
subject: 'Test Email',
text: 'Hello, this is a test email sent using Nodemailer and OAuth2!',
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Email sent: ' + info.response);
});
};
sendEmail().catch(console.error);
const { ConfidentialClientApplication } = require('@azure/msal-node');
const config = {
auth: {
clientId: 'YOUR_CLIENT_ID',
authority: 'https://login.microsoftonline.com/YOUR_TENANT_ID',
clientSecret: 'YOUR_CLIENT_SECRET',
}
};
const cca = new ConfidentialClientApplication(config);
const getAccessToken = async () => {
const result = await cca.acquireTokenByClientCredential({
scopes: ['https://graph.microsoft.com/.default'],
});
return result.accessToken;
};
const sendEmail = async () => {
const accessToken = await getAccessToken();
const transporter = nodemailer.createTransport({
service: 'Outlook365',
auth: {
type: 'OAuth2',
user: 'email address removed for privacy reasons',
accessToken: accessToken,
}
});
const mailOptions = {
from: 'email address removed for privacy reasons',
to: 'email address removed for privacy reasons',
subject: 'Test Email',
text: 'Hello, this is a test email sent using Nodemailer and OAuth2!',
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Email sent: ' + info.response);
});
};
sendEmail().catch(console.error);
- onezerocomSep 27, 2024Copper Contributor
malik786 hello!I used your approach but encountered the “OrganizationFromTenantGuidNotFound” error. Are there any prerequisites, such as a license. Looking forward to your reply, thanks!
- onesbenrhaimeJul 20, 2024Copper ContributorThank you, it worked. I removed the credential access for Azure and it worked.