我的目标是使用来自JavaScript的Gmail API REST调用的邮件传递系统,所有邮件都不同,邮件列表可以覆盖数千人,因此我无法使用CC字段。我遇到的问题是我在发送操作完成后得到的消息重复。我发送了100条消息,并在接下来的7-20分钟内收到超过120条消息。
function authorize(credentials) {
var clientSecret = credentials.web.client_secret;
var clientId = credentials.web.client_id;
var redirectUrl = {redirect_uri};
var auth = new googleAuth();
var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
fs.readFile(TOKEN_PATH, function(err, token) {
if (err) {
getNewToken(oauth2Client, sendMessagePrepair);
} else {
oauth2Client.credentials = JSON.parse(token);
sendMessagePrepair(oauth2Client);
}
});
}
function getNewToken(oauth2Client, callback) {
var authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
});
console.log('Authorize this app by visiting this url: ', authUrl);
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter the code from that page here: ', function(code) {
rl.close();
oauth2Client.getToken(code, function(err, token) {
if (err) {
console.log('Error while trying to retrieve access token', err);
return;
}
oauth2Client.credentials = token;
storeToken(token);
callback(oauth2Client);
});
});
var counter = 0;
var before_counter = 0;
var SEND_COUNT = 100;
function sendM(post_req, callback) {
timeout = before_counter * 100;
before_counter++;
setTimeout(function(){
post_req.write(JSON.stringify({ "raw": mail }));
post_req.end();
}, timeout);
}
function sendMessagePrepair(oauth2Client) {
var start = new Date().getTime();
mail = new Buffer("Content-Type: text/html; charset=\"UTF-8\"\n" +
"MIME-Version: 1.0\n" +
"From: {From}\n" +
"To: {To}\n" +
"Subject: {Subject}\n\n" +
messages
).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');
var post_options = {
hostname: 'www.googleapis.com',
port: '443',
path: '/gmail/v1/users/me/messages/send',
method: 'POST',
headers: {
"Authorization": 'Bearer ' + oauth2Client.credentials.access_token,
"Content-Type" : "application/json"
}
};
var post_req = [];
for(var idx = 0; idx < SEND_COUNT; idx++)
post_req.push(http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
counter++;
if(counter == SEND_COUNT)
process.exit();
});
}));
async.map(post_req, sendM, function(e, r){
console.log(r);
});
}
有时我会收到来自Google服务器的“限制已达到”或“BackendError”RetCode,但即便如此,我仍会收到更多电子邮件,然后才会发送。我使用Node.JS Google Client Library获得了相同的行为:
var gmail = google.gmail('v1');
gmail.users.messages.send({....},callback);
BackendError消息:
回复:{“error”:{“errors”:[{ “域名”:“全球”, “reason”:“backendError”, “message”:“Backend Error”}],“code”:500,“message”:“Backend Error”}}
我的问题是:有人有同样的问题吗?有没有解决方法?我无法在网上找到关于这种行为的任何内容。