2017-06-05 59 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

鏈接是不是答案。請添加一些代碼和解釋 –