我和这篇文章有完全相同的问题:使用 nodemailer 和 gmail 发送多封电子邮件
I have exactly the same issue as this post : Send multipe emails using nodemailer and gmail
使用 nodemailer 和 gmail 发送过多电子邮件时,我收到 421 错误,指的是并发会话过多.
When sending too many emails with nodemailer and gmail, I get a 421 error, referring to too many concurrent sessions.
如何避免打开过多会话?
What can i do to avoid opening too many sessions ?
我已经联系了 google,他确认我没有被任何限制阻止(我没有达到每日邮件的数量并且邮件/分钟没有限制).在发送新邮件之前,我尝试等待每封邮件发送完毕;在每封邮件中创建和关闭新的传输,但我在大约第 100 封电子邮件后不断收到此错误.
I already contact google who confirm me I wasn't blocked by any limitation ( I didn't reach the number of daily mail and there is no limit on mail/minute). I tried to wait for each mail to be sent before sending a new one; to create and close new transport at each mail, but I keep getting this error after approximately the 100th email.
这里是完整的错误:
{ [Error: Mail command failed: 421 4.7.0 Try again later, closing connection. (MAIL) e17sm2124566ede.14 - gsmtp] code: 'EENVELOPE', response: '421 4.7.0 Try again later, closing connection. (MAIL) e17sm2124566ede.14 - gsmtp', responseCode: 421, command: 'MAIL FROM' }还有我的代码:
Nodemailer 设置:
Nodemailer settings :
function setMailTransport () { return nodemailer.createTransport(smtpTransport({ service: 'gmail', ignoreTLS: true, auth: { xoauth2: xoauth2.createXOAuth2Generator({ user: 'xxxxxx', clientId: 'xxxxxx', clientSecret: 'xxxxxx', refreshToken: 'xxxxxx' }) } })) }发送独特的邮件:
async function sendEmail (mail) { // mail is an object {from, to, subject, text, html} const transport = setMailTransport() try { await transport.sendMail(mail) await transport.close() return 1 } catch (err) { console.log(err) await transport.close() return 0 } }递归 async/await 函数在发送新邮件之前等待邮件发送:
Recursive async/await function to wait mail be sent before sending a new one :
async function sendAlerts (mails, index, numberOfMailSent) { // mails is an array of mail object, index start at 0 // numberOfMailSent is just a counter to know how many mails have been sent if (index >= mails.length) return numberOfMailSent const mail = mails[position] const newMailSent = await sendEmail(mail) return sendAlerts(mails, index + 1, numberOfMailSent + newMailSent) }知道我可能在哪里出错或以任何其他方式发送 100 多封邮件吗?
Any idea of where I could have been wrong or on any other way to send more than 100 mails ?
推荐答案您应该将 pool 参数添加到您的 nodemailer 传输器配置对象中:
You should add the pool parameter to your nodemailer transporter config object:
export const transporter = nodemailer.createTransport({ service: 'gmail', auth: {}, pool: true });有关其他配置选项,您可以查看此处
For additional configuration options, you can check here
更多推荐
nodemailer gmail 连接因大量邮件而关闭
发布评论