Search code examples
javascriptnode.jsemailnodemailer

Error: connect ETIMEDOUT xxx.xx.xxx.xxx:587 Nodemailer script stopped working with outlook throws


A few months ago I created this script to help me send multiple mails at once instead of sending them one by one, and it was working perfectly till now.

For this script, I've used dotenv, nodemailer, and nodemon (only in development, when I finished it I started using npm run start to run it)

    (function main() {

    const { contacts } = contacts_list;

    try {

        const { MAIL_ACCOUNT, MAIL_PASSWORD } = process.env;

        const transport = nodemailer.createTransport({

            // Uses 'pool' attribute: The same connection is used to send up to 100 mails before being disposed.
            pool: true,
            // Sets the number of max connections per transport. Microsoft accepts up to 1 parallel connection for the same client.
            maxConnections: 1,
            logger: true,
            debug: true,
            service: "hotmail",
            auth: {
                user: MAIL_ACCOUNT,
                pass: MAIL_PASSWORD
            },
            tls: { rejectUnauthorized: false }

        })

        // Change mail parameters such as subject, recipients, content, etc
        const mailParams = {
            from: `test <${MAIL_ACCOUNT}>`,
            to: '',
            attachments: [],
            subject: `test`,
            text: `test`,
            html: `<h1>test</h1>`
        }

        // Mail every contact once at a time with its corresponding attachments
        contacts.forEach(async (contact) => {

            mailParams.to = contact.email;
            mailParams.attachments = [...contact.attachments];

            await transport.sendMail(mailParams);

        })


    } catch (err) {
        
        console.error(err);

    }

})();

I've already scanned for blocked ports, disabled firewall when trying to use it and antivirus as well. None of these approaches worked.

It throws the following error:

node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

Error: connect ETIMEDOUT xxx.xx.xxx.xxx:587
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16) {
  errno: -4039,
  code: 'ESOCKET',
  syscall: 'connect',
  address: 'xxx.xx.xxx.xxx',
  port: 587,
  command: 'CONN'
}

Logger prints the following lines:

[2022-02-03 01:39:22] DEBUG Creating transport: nodemailer (6.7.2; +https://nodemailer.com/; SMTP (pool)/6.7.2[client:6.7.2])
[2022-02-03 01:39:22] DEBUG Sending mail using SMTP (pool)/6.7.2[client:6.7.2]
[2022-02-03 01:39:22] DEBUG Sending mail using SMTP (pool)/6.7.2[client:6.7.2]
[2022-02-03 01:39:22] INFO  [#1] Created new pool resource #1
[2022-02-03 01:39:22] DEBUG [#1] Assigned message <567cc726-524b-0bda-5a52-698109ae7d78@hotmail.com> to #1 (1)
[2022-02-03 01:39:22] DEBUG [nHXJGQWMbA] Resolved smtp.live.com as xxx.xx.xxx.xxx [cache miss]
[2022-02-03 01:39:43] ERROR [nHXJGQWMbA] connect ETIMEDOUT xxx.xx.xxx.xxx:587
[2022-02-03 01:39:43] ERROR [#1] Pool Error for #1: connect ETIMEDOUT xxx.xx.xxx.xxx:587
[2022-02-03 01:39:43] ERROR Send Error: connect ETIMEDOUT xxx.xx.xxx.xxx:587
[2022-02-03 01:39:43] DEBUG [nHXJGQWMbA] Closing connection to the server using "destroy"
[2022-02-03 01:39:43] INFO  [#1] Connection #1 was closed

If you know how to solve it, i'll be very grateful if you lend me a hand!

SOLUTION:

Finally, I found the solution thanks to @Marco Strahilov 's answer in other post. When instantiating the transport, set the service property to 'smtp-mail.outlook.com' instead of hotmail. I don't know for sure why 'hotmail' stopped working nor why 'stmp-email.outlook.com' works now.


Solution

  • Finally, I found the solution thanks to @Marco Strahilov 's answer in other post.

    When instantiating the transport, set the service property to 'smtp-mail.outlook.com' instead of hotmail. I don't know for sure why 'hotmail' stopped working.