在 NodeJS 中使用 Microsoft 365 电子邮件服务器发送电子邮件

Har*_*nde 6 node.js nodemailer

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

我在发送电子邮件时遇到 EAUTH 错误,请检查图像是否有错误。[1]: https: //i.stack.imgur.com/snt3T.jpg

Ter*_*nox 4

此代码应该执行您想要的操作,您需要设置密码来测试它。

如果密码不正确,您将收到错误消息:

错误:无效登录:535 5.7.3 身份验证失败消息。

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");
Run Code Online (Sandbox Code Playgroud)