Send Email Using Microsoft 365 Email Server In NodeJS

Viewed 3487
let transporter = nodemailer.createTransport({
    service: "Outlook365",
    host: 'smtp.office365.com',
    port: 587,
    tls: {
        ciphers:'SSLv3'
    },
    auth: {
        user: 'username',
        pass: 'password'
    }
});

I have an EAUTH error while sending an email, please check the image for error. [1]: https://i.stack.imgur.com/snt3T.jpg

3 Answers

This code should do what you wish, you'll need to set your password to test this.

If the password is incorrect, you'll get an error:

Error: Invalid login: 535 5.7.3 Authentication unsuccessful message.

const nodemailer = require('nodemailer');

// Set this from config or environment variable.
const PASSWORD = '....';

async function send365Email(from, to, subject, html, text) {
    try { 
        const transportOptions = {
            host: 'smtp.office365.com',
            port: '587',
            auth: { user: from, pass: PASSWORD },
            secureConnection: true,
            tls: { ciphers: 'SSLv3' }
        };
    
        const mailTransport = nodemailer.createTransport(transportOptions);
    
        await mailTransport.sendMail({
            from,
            to,
            replyTo: from,
            subject,
            html,
            text
        });
    } catch (err) { 
        console.error(`send365Email: An error occurred:`, err);
    }
}

send365Email("from@example.com", "to@example.com", "Subject", "<i>Hello World</i>", "Hello World");
Related