Email service for office365

I am trying to use a Mongodb Atlas function to send an through an Office365 SMTP service without success. I have seen some threads but nothing seems to work. Any successes.

Sending emails from MongoDB Atlas using an Office365 SMTP service can be achieved. You can create a database trigger in MongoDB Atlas that responds to specific events (e.g., when a new order is placed). In the trigger function, you can invoke an external service (such as Node.js) to send an email via SMTP.

const nodemailer = require(‘nodemailer’);

// Create a transporter using Office365 SMTP settings
const transporter = nodemailer.createTransport({
host: ‘smtp.office365.com’,
port: 587,
secure: false,
auth: {
user: ‘your_office365_email@example.com’,
pass: ‘your_office365_password’,
},
});

// Define the email content
const mailOptions = {
from: ‘your_office365_email@example.com’,
to: ‘recipient@example.com’,
subject: ‘Hello from MongoDB Atlas’,
text: ‘This is a test email sent from MongoDB Atlas.’,
};

// Send the email
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error(‘Error sending email:’, error);
} else {
console.log(‘Email sent:’, info.response);
}
});

Robert: T hanks for the response. Actually, I have been doing exactly as you stipulated. In fact with the exception of the sample data versus live data, the code is the same. Unfortunately, I get the response: Error: Error initiating TLS - [object Object].
Any thoughts?

Actually I’m also having same issue. It works well if the email if configured with gmail looks like some issue with outbound traffic config which is blocking connection to microsoft 365