【问题标题】:Office 365 Oauth with Nodemailer Can't create new access tokenOffice 365 Oauth with Nodemailer Can\'t create new access token
【发布时间】:2022-12-27 14:41:48
【问题描述】:

I am trying to use Nodemailer in express server with Oauth from Office 365 but I am getting Can't create new access token for user and {"code": "EAUTH", "command": "AUTH XOAUTH2" error. It seems like nodemailer is unable to obtain either the access token and refresh token and the user is not being authenticated to send mails.

  const transporter = nodemailer.createTransport({
    host: "smtp.office365.com",
    port: 587,
    secure: false,
    tls: {
      ciphers: "SSLv3"
    },
    requireTLS: true,
    auth: {
      type: "OAuth2",
      user: process.env.SENDER_EMAIL,
      clientId: "CLIENT_ID",
      clientSecret: "CLEINT_SECRET",
      accessUrl: "https://login.microsoftonline.com/SOMETHING_SECRET_HERE/oauth2/v2.0/authorize"
      // pass: process.env.SENDER_PASSWORD
    }
  });

I am not familiar with OAuth 2.0 with Office 365 to begin with so there could be some configurations error etc. The nodemailer works fine if I use my account credentials though. Can someone please suggest me something to try out or let me know if my config is wrong.

【问题讨论】:

    标签: express oauth-2.0 azure-active-directory office365 nodemailer


    【解决方案1】:

    Please note that,accessUrlis an endpoint for requesting new access token.

    As you have passed Authorization endpoint, please try changing it toOAuth 2.0 token endpoint (v2).

    You can find thisendpoint valuein the Portal like below:

    Go to Azure Portal -> Azure Active Directory -> App Registrations -> Your App

    Alternatively, you can make use of Microsoft Graph API to send the mails like below:

    Make sure to haveMail.Sendpermission consented before using the below query:

    POST https://graph.microsoft.com/v1.0/me/sendMail
    
    {
    "message": {
    "subject": "Regarding leave approval",
    "body": {
    "contentType": "Text",
    "content": "Please approve my leave request."
    },
    
    "toRecipients": [
    {
    "emailAddress": {
    "address": "XXXXX"
    }
    }
    ],
    
    "ccRecipients": [
    {
    "emailAddress": {
    "address": "XXXXX"
    }
    }
    ]
    },
    "saveToSentItems": "false"
    }
    

    Response:

    The mail was successfully triggered like below:

    For more in detail, pleasereferthe below links:

    Send mail - Microsoft Graph v1.0 | Microsoft Docs

    Modern Oauth2 authentication for sending mails using Nodemailer nodejs by Sivaprakash-MSFT

    【讨论】:

      【解决方案2】:

      Did you figure out how to upgrade to Oauth2 using nodemailer for outlook?

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-01
      • 1970-01-01
      • 2022-12-26
      • 2017-10-25
      • 1970-01-01
      • 2016-11-06
      • 2012-03-20
      • 2022-08-16
      相关资源
      最近更新 更多