3

I am trying to send an email from office 365 outlook account using oauth2

but I am getting following error

Invalid login: 451 4.7.0 Temporary server error. Please try again later. PRX4  [MA1PR01CA0110.INDPRD01.PROD.OUTLOOK.COM]

Here is my code

let sendEmail: (accessToken) => {
    return new Promise((resolve, reject) => {
        let options = {
            "to": "xxx",
            "subject": "hello from nodemailer",
            "text": "Hello from nodemailer again"
            "from": 'xxx'
        }
        let transporter = nodemailer.createTransport({
            host: 'smtp.office365.com',
            port: 587,
            secure: true,
            auth: {
                type: 'OAuth2',
                user: options.from,
                accessToken: accessToken
            },
            tls: { ciphers: 'SSLv3' }
        })
        transporter.sendMail(options, (err, info) => {
            return err ? reject(err) : resolve(info);
        })
    })
}  

Also tried with port: 465 but getting error

connect ETIMEDOUT 40.100.136.18:465

I have also given 'Send mail as a user' permission while creating an access token.

Am I missing something? please suggest the solution.

Sam
  • 795
  • 2
  • 18
  • 31

2 Answers2

1

Based on the code, it seems that you were mixing the SMTP authentication with Oauth2.

After some researching, Office 365 doesn't support this(refer this thread). To work with SMTP you need to provide the username/password to connect the server. You can refer this thread for the detailed code sample to connect SMTP server for Office365.

And if you want to retrieve the mails through the Oauth authentication, you can refer this blog. And the author(Jason Johnston [MSFT]) also provide the full code sample on his's GitHub.

Fei Xue
  • 14,369
  • 1
  • 19
  • 27
0

I had the same 4.7.0 Temporary server error issue and it wasn't due to same issue as OP above or DNS (as I found this can be an issue in other searches if you have more than one DNS set) but the time was off by over 10 minutes!

My mail server is running on VM so found the host time was actually off. Fixed both the host and mail server and that fixed the issue for me.

Posting this here since I found this post and I might help somebody else in the future.

ouflak
  • 2,458
  • 10
  • 44
  • 49
Cradz
  • 1
  • 1