'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.stack.imgur.com/snt3T.jpg



Solution 1:[1]

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");

Solution 2:[2]

You can find the detailed information here:

https://developer.microsoft.com/en-us/graph/quick-start?code=M.R3_BAY.822b5ade-d816-85bb-ec94-8c349cdfca4b&state=option-node

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Shameel Uddin