Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Email Using Microsoft 365 Email Server In NodeJS

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.sstatic.net/snt3T.jpg

like image 859
Harshal Deshpande Avatar asked Aug 07 '26 13:08

Harshal Deshpande


1 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("[email protected]", "[email protected]", "Subject", "<i>Hello World</i>", "Hello World");
like image 81
Terry Lennox Avatar answered Aug 10 '26 15:08

Terry Lennox