2017-06-05 57 views
2

封闭我有完全一样的问题,因为这个帖子: Send multipe emails using nodemailer and gmailnodemailer Gmail连线与大量邮件

当发送带有nodemailer和Gmail太多的电子邮件,我得到一个421错误,指的是太多的并发会话。

我该怎么做才能避免打开太多会话?

我已经联系谷歌谁确认我我没有被任何限制(我没有达到每日邮件的数量和邮件/分钟没有限制)封锁。 我试图在发送新邮件之前等待每封邮件的发送;在每封邮件中创建并关闭新的传输,但在大约第100封电子邮件后,我一直收到此错误。

这里完整的错误:

{ [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设置:

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 
    } 
} 

递归异步/ AWAIT functi在等待邮件发送一个新的前发送:

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个邮件的任何想法?

回答

-1

使用池SMTP:https://nodemailer.com/smtp/pooled/

If pooling is used then Nodemailer keeps a fixed amount of connections open and sends the next message once a connection becomes available. It is mostly useful when you have a large number of messages that you want to send in batches or your provider allows you to only use a small amount of parallel connections.

+0

链接是不是答案。请添加一些代码和解释 –