[英]Send multipe emails using nodemailer and gmail
I am trying to send an email to multiple recipients ( about 3.000 ). 我正在尝试向多个收件人(约3.000)发送电子邮件。 All emails are stored in my DB ( Mongo ).
所有电子邮件都存储在我的数据库(Mongo)中。 So I make a query that return all the email addresses, and I use async to send all the emails, like:
因此,我进行查询以返回所有电子邮件地址,并使用async发送所有电子邮件,例如:
function _sendEmail(params, callback) {
async.each(params.email, function(user, cb) {
const mailOptions = {
from: sender
to: user,
subject: Subject,
text: 'Hello Word',
};
app.transporter.sendMail(mailOptions, function(err, response) {
if(err) console.log(err);
else console.log(response);
cb();
});
}, callback);
}
I am creating my nodemailer transporte in my app.js, ,like so: 我正在app.js中创建nodemailer transporte,如下所示:
const transporter = nodemailer.createTransport(smtpTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: senderMail,
pass: senderMailPassword
}
}));
When I try to send this to only 10 mails, it works just fine, but when I try to send to all the emails in my DB, I am getting this error a bunch of times: 当我尝试仅将其发送到10个邮件时,它工作正常,但是当我尝试将其发送到数据库中的所有电子邮件时,却多次出现此错误:
{ [Error: Data command failed: 421 4.7.0 Temporary System Problem. Try again later (WS). g32sm7412411qtd.28 - gsmtp]
code: 'EENVELOPE',
response: '421 4.7.0 Temporary System Problem. Try again later (WS). g32sm7412411qtd.28 - gsmtp',
responseCode: 421,
command: 'DATA' }
Am I missing something? 我想念什么吗? Do I need to set something to be able to send lots os emails in a small period of time?
我是否需要设置一些内容以便能够在短时间内发送大量邮件? I am using a gmail account to do that!
我正在使用一个Gmail帐户来做到这一点!
Thanks in advance! 提前致谢!
From Gmail : 421 SMTP Server error: too many concurrent sessions 来自Gmail:421 SMTP服务器错误:并发会话过多
You may handle your send differently : 您可以用不同的方式处理发送:
wait to close the session between each sending 等待关闭每次发送之间的会话
send by bunch of mail 通过一堆邮件发送
The best way is to manage to not exceed the limit of 10 session in the same time :) 最好的方法是设法同时不要超过10个会话的限制:)
It is because you are attempting to create a new smtp connection for each email. 这是因为您试图为每个电子邮件创建一个新的smtp连接。 You need to use SMTP pool.
您需要使用SMTP池。 Pooled smtp 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.
当您有大量要批量发送的消息,或者您的提供程序仅允许使用少量并行连接时,池化smtp最为有用。
const transporter = nodemailer.createTransport(smtpTransport({
host: 'smtp.gmail.com',
port: 465,
pool: true, // This is the field you need to add
secure: true,
auth: {
user: senderMail,
pass: senderMailPassword
}
})); }));
You can close the pool as 您可以关闭游泳池
transporter.close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.