Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send email with my company domain using Nodemailer?

This is my code: I tried using an outlook account

const transporter = nodemailer.createTransport({
  host: 'smtp.office365.com',
  auth: {
    user: '[email protected]',
    pass: 'password'
  }
});

transporter.verify(function(error, success) {
  if (error) {
    console.log(error);
  } else {
    console.log('Server is ready to take our messages');
  }
});

Im not sure what is the problem over here. These are the nodemailer logs:

[2020-03-17 09:55:34] DEBUG Creating transport: nodemailer (6.4.5; +https://nodemailer.com/; SMTP/6.4.5[client:6.4.5])
[2020-03-17 09:55:34] DEBUG [JAejdt6KMrw] Resolved smtp.office365.com as 40.100.54.194 [cache miss]
[2020-03-17 09:55:35] INFO  [JAejdt6KMrw] Connection established to 40.100.54.194:587
[2020-03-17 09:55:35] INFO  [JAejdt6KMrw] Connection upgraded with STARTTLS
[2020-03-17 09:55:36] DEBUG [JAejdt6KMrw] SMTP handshake finished
[2020-03-17 09:55:41] INFO  [JAejdt6KMrw] User "[email protected]" failed to authenticate
[2020-03-17 09:55:41] DEBUG [JAejdt6KMrw] Closing connection to the server using "end"
Error: Invalid login: 535 5.7.3 Authentication unsuccessful [HK2PR04CA0060.apcprd04.prod.outlook.com]
    at SMTPConnection._formatError (C:\Users\Nonoy\Documents\sample_apps\nodemailer_outlook_integration\node_modules\nodemailer\lib\smtp-connection\index.js:784:19)
    at SMTPConnection._actionAUTHComplete (C:\Users\Nonoy\Documents\sample_apps\nodemailer_outlook_integration\node_modules\nodemailer\lib\smtp-connection\index.js:1523:34)
    at SMTPConnection.<anonymous> (C:\Users\Nonoy\Documents\sample_apps\nodemailer_outlook_integration\node_modules\nodemailer\lib\smtp-connection\index.js:1481:18)
    at SMTPConnection._processResponse (C:\Users\Nonoy\Documents\sample_apps\nodemailer_outlook_integration\node_modules\nodemailer\lib\smtp-connection\index.js:942:20)
    at SMTPConnection._onData (C:\Users\Nonoy\Documents\sample_apps\nodemailer_outlook_integration\node_modules\nodemailer\lib\smtp-connection\index.js:749:14)
    at TLSSocket.SMTPConnection._onSocketData (C:\Users\Nonoy\Documents\sample_apps\nodemailer_outlook_integration\node_modules\nodemailer\lib\smtp-connection\index.js:195:44)
    at TLSSocket.emit (events.js:223:5)
    at addChunk (_stream_readable.js:309:12)
    at readableAddChunk (_stream_readable.js:290:11)
    at TLSSocket.Readable.push (_stream_readable.js:224:10) {
  code: 'EAUTH',
  response: '535 5.7.3 Authentication unsuccessful [HK2PR04CA0060.apcprd04.prod.outlook.com]',
  responseCode: 535,
  command: 'AUTH LOGIN'
}
[2020-03-17 09:55:41] INFO  [JAejdt6KMrw] Connection closed

PS: my company domain is hosted on Microsoft

like image 931
Juan Miguel Ladisla Avatar asked Sep 12 '25 02:09

Juan Miguel Ladisla


1 Answers

Thank you for your answers guys (tried them all). But I found the solution.

In my office365 account, I went to the settings, Security & Privacy, Additional security verification, clicked on Create and manage app passwords. Then it will let you generate an app password that you will then use as your nodemailer auth config password. This will also bypass the 2-factor authentication if you have one like mine.

screenshot of settings window here

Note that you must copy the generated password immediately. It will only let you copy the password once. Upon closing the window, you can't view it again.

Hope this helps!

like image 128
Juan Miguel Ladisla Avatar answered Sep 13 '25 16:09

Juan Miguel Ladisla