1

I'm creating a email verification system for my website and I'm using nodemailer to send the emails

However I'm concerned that the email won't be sent when multiple users ask for email verification

So to check for this, I created a simulation code which looks like this:

const express = require('express');
const app=express();

app.use(express.json());

const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
  host: "smtp-mail.outlook.com", 
  secureConnection: false, 
  port: 587,
  pool:true,
  maxConnections:2,
  tls: {
     ciphers:'SSLv3',
     rejectUnauthorized: false
  },
  auth: {
      user: 'generic-email@outlook.com',
      pass: 'generic password'
  }
  });

app.get('/',async (req,res)=>{

for(var i=0;i<10;i++)
{
  transporter.sendMail({
    from: 'generic-email@outlook.com', // sender address
    to: `generic-client@gmail.com`, // list of receivers
    subject: "testing", // Subject line
    text: "testing?", // plain text body
    html: "<b>TESTING</b>", // html body
  },(err,info)=>{
    if(err)
     console.log('ERROR=>',err);
    else
     console.log('INFO=>',info);
  });
}
return res.send('Emails sent')
});


app.listen(3000,()=>{
    console.log('Listening at Port 3000...');
 });

as far as my knowledge goes the above code is stacking up 10 emails (due to the pool:true) and sending 2 concurrently (due to the maxConnections:2 ) at a time and theoretically it should work as according to Microsoft outlook docs 3 concurrent connections are allowed

The service has various limits to prevent abuse and to ensure fair use. An additional limit is being added. Under the new limit, up to three concurrent connections are allowed to send email messages at the same time. If an application tries to send more than three messages at the same time by using multiple connections, each connection will receive the following error message:

but on running the code I randomly get this error for some of the sent emails:

Error: Message failed: 432 4.3.2 Concurrent connections limit exceeded. Visit https://aka.ms/concurrent_sending for more information. [Hostname=SG2PR01MB2186.apcprd01.prod.exchangelabs.com]
    at SMTPConnection._formatError (C:\Users\name\node_modules\nodemailer\lib\smtp-connection\index.js:774:19)
    at SMTPConnection._actionSMTPStream (C:\Users\name\node_modules\nodemailer\lib\smtp-connection\index.js:1651:34)
    at SMTPConnection.<anonymous> (C:\Users\name\node_modules\nodemailer\lib\smtp-connection\index.js:1136:22)
    at SMTPConnection._processResponse (C:\Users\name\node_modules\nodemailer\lib\smtp-connection\index.js:932:20)
    at SMTPConnection._onData (C:\Users\name\node_modules\nodemailer\lib\smtp-connection\index.js:739:14)
    at TLSSocket.SMTPConnection._onSocketData (C:\Users\name\node_modules\nodemailer\lib\smtp-connection\index.js:189:44)
    at TLSSocket.emit (events.js:376:20)
    at addChunk (internal/streams/readable.js:309:12)
    at readableAddChunk (internal/streams/readable.js:284:9)
    at TLSSocket.Readable.push (internal/streams/readable.js:223:10) {       
  code: 'EMESSAGE',
  response: '432 4.3.2 Concurrent connections limit exceeded. Visit https://aka.ms/concurrent_sending for more information. [Hostname=SG2PR01MB2186.apcprd01.prod.exchangelabs.com]',
  responseCode: 432,
  command: 'DATA'
}

so what am I doing wrong here? and how do I make sure I can handle multiple email requests efficiently without nodemailer breaking down?

  • An answer to a very similar question yesterday is here [nodemailer concurrent connections limit exceeded](https://stackoverflow.com/questions/67846898/node-js-nodemailer-express-outlook-smtp-host-concurrent-connections-limit-exce/67847042#67847042). It seems that answer would apply here too. If you want to use three simultaneous connections (and no more), then see the comment and link in that about using `mapConcurrent()` to control your iteration with only 3 operations in flight at once. – jfriend00 Jun 06 '21 at 04:09
  • 1
    yes what you've sent does seem relevant to the question I'm asking but I'm having a hard time trying to understand it. Also the question you've sent asks how to send an array of emails at the same time however I'm asking how to handle multiple requests over due time (may or may not be same). could you kindly write a new answer on this question so I can understand in detail if you don't mind :) –  Jun 06 '21 at 04:25
  • well in the answer you've written you've used `await` so I think you're only handling 1 connection at a time? also how does `email.json_agg` work? also I can't make out comparingly what I've done wrong in my code –  Jun 06 '21 at 04:31
  • Read the reference to `mapConcurrent()` and the link it points to. That allows you to control yourself how many operations are in flight at the same time. I don't know how `maxConnections` works in `nodemailer`. The doc for that is fairly pointless. It doesn't say whether you get an error when you exceed that number or whether it automatically queues operations to avoid exceeding that. Worthless doc. You would have to go look at nodemailer's code to see what it actually does with that setting. – jfriend00 Jun 06 '21 at 04:34
  • @jfriend00 https://stackoverflow.com/questions/67852677/sending-concurrent-emails-with-nodemailer-in-node-js/67852711?noredirect=1#comment119932264_67852711 in the comments you can see it says that the `maxConnections` queues the operations so that's the basis of my code –  Jun 06 '21 at 04:37
  • 1
    Yeah, [here in the nodemailer code](https://github.com/nodemailer/nodemailer/blob/1750c0ff89ab79161f722b0cd35381cbc39fee46/lib/ses-transport/index.js#L61), it appears to put it in a queue. I have no idea why yours isn't getting queued appropriately. You can step into it in the debugger and see what's happening. You can just debug it yourself. – jfriend00 Jun 06 '21 at 04:39
  • 1
    @jfriend00 okay after I changed `maxConnections:1` my code worked like a charm even for a loop of 100 and for multiple get requests but it still doesn't answer why I can't have `maxConnections:1` –  Jun 06 '21 at 04:57

0 Answers0