Forum Discussion

milos95's avatar
milos95
Copper Contributor
Sep 05, 2023

How to send emails thru nodemailer (node.js) from outlook? SMTP Disabled

Hello, I'm writing a small backend node.js application from which I'd like to send emails. I set everything up in the following fashion:

 

  try {
    const transporter = nodemailer.createTransport({
      host: "smtp.office365.com",
      port: 587,
      secure: false, // STARTTLS
      auth: {
        user: "email address removed for privacy reasons",
        pass: "secret-password",
      },
    });

    const mailOptions = {
      from: senderEmail,
      to: recipientEmail,
      subject: "Quote from Anonymous",
      text: JSON.stringify(storedFormData, null, 2),
    };

    await transporter.sendMail(mailOptions);
    res.json({ message: "Email sent successfully." });

 


However, no matter how I change the structure of the code I keep getting the following error:

code: 'EAUTH',
response: '535 5.7.139 Authentication unsuccessful, SmtpClientAuthentication is disabled for the Tenant. Visit https://aka.ms/smtp_auth_disabled for more information. [FR2P281CA0043.DEUP281.PROD.OUTLOOK.COM 2023-09-05T20:12:25.431Z 08DBACD4F0020982]',
responseCode: 535,
command: 'AUTH LOGIN'

So I guess my question is, how to enable SMTP for tenant?

  • malik786's avatar
    malik786
    Copper Contributor
    const nodemailer = require('nodemailer');
    const { ConfidentialClientApplication } = require('@azure/msal-node');

    const config = {
    auth: {
    clientId: 'YOUR_CLIENT_ID',
    authority: 'https://login.microsoftonline.com/YOUR_TENANT_ID',
    clientSecret: 'YOUR_CLIENT_SECRET',
    }
    };

    const cca = new ConfidentialClientApplication(config);

    const getAccessToken = async () => {
    const result = await cca.acquireTokenByClientCredential({
    scopes: ['https://graph.microsoft.com/.default'],
    });
    return result.accessToken;
    };

    const sendEmail = async () => {
    const accessToken = await getAccessToken();

    const transporter = nodemailer.createTransport({
    service: 'Outlook365',
    auth: {
    type: 'OAuth2',
    user: 'email address removed for privacy reasons',
    accessToken: accessToken,
    }
    });

    const mailOptions = {
    from: 'email address removed for privacy reasons',
    to: 'email address removed for privacy reasons',
    subject: 'Test Email',
    text: 'Hello, this is a test email sent using Nodemailer and OAuth2!',
    };

    transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
    return console.log(error);
    }
    console.log('Email sent: ' + info.response);
    });
    };

    sendEmail().catch(console.error);
    • onesbenrhaime's avatar
      onesbenrhaime
      Copper Contributor
      Thank you, it worked. I removed the credential access for Azure and it worked.
    • onezerocom's avatar
      onezerocom
      Copper Contributor

      malik786 hello!I used your approach but encountered the “OrganizationFromTenantGuidNotFound” error. Are there any prerequisites, such as a license. Looking forward to your reply, thanks!

  • kushagra980's avatar
    kushagra980
    Copper Contributor

    milos95 
    hello milos , i am too working on this and facing similar issule , kindly let me know if you solved this problem 

  • kushagra980's avatar
    kushagra980
    Copper Contributor

    hello , this did not work for me , i used an alternative which is email communication  service ,which is used to connect to domain and then use but keep in mind the email will be email address removed for privacy reasons,

    if you want i can help you with code in nodejs . but this problem still exist to  .

Resources