我按照本教程设置了可以发送电子邮件的node.js
应用,但是当我运行代码时出现以下错误:
错误:' raw' RFC822有效负载消息字符串或通过/ upload / * URL上传消息 代码:400, 错误: [{domain:' global', 原因:' invalidArgument', 消息:' \' raw \' RFC822有效负载消息字符串或通过/ upload / * URL上传消息' }]
这是我的代码:
var fs = require('fs');
var googleAuth = require('google-auth-library');
var {google} = require('googleapis');
function getOAuth2Client(cb) {
// Load client secrets
fs.readFile('client_secret.json', function(err, data) {
if (err) {
return cb(err);
}
var credentials = JSON.parse(data);
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
var redirectUrl = credentials.installed.redirect_uris[0];
var auth = new googleAuth();
var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
// Load credentials
fs.readFile('gmail-credentials.json', function(err, token) {
if (err) {
return cb(err);
} else {
oauth2Client.credentials = JSON.parse(token);
return cb(null, oauth2Client);
}
});
});
}
function sendSampleMail(auth, cb) {
var gmailClass = google.gmail('v1');
var email_lines = [];
email_lines.push('From: "test" <my_email@gmail.com>');
email_lines.push('To: my_email@gmail.com');
email_lines.push('Content-type: text/html;charset=iso-8859-1');
email_lines.push('MIME-Version: 1.0');
email_lines.push('Subject: this would be the subject');
email_lines.push('');
email_lines.push('And this would be the content.<br/>');
email_lines.push('The body is in HTML so <b>we could even use bold</b>');
var email = email_lines.join('\r\n').trim();
var base64EncodedEmail = new Buffer(email).toString('base64');
base64EncodedEmail = base64EncodedEmail.replace(/\+/g, '-').replace(/\//g, '_');
gmailClass.users.messages.send({
auth: auth,
userId: 'me',
resource: {
raw: base64EncodedEmail
}
}, cb);
}
getOAuth2Client(function(err, oauth2Client) {
if (err) {
console.log('err:', err);
} else {
sendSampleMail(oauth2Client, function(err, results) {
if (err) {
console.log('err:', err);
} else {
console.log(results);
}
});
}
});
我做错了吗?