通过 GMail API 从我的 Javascript 应用程序发送电子邮件 - 邮件出现在 GMail 已发送列表中,但未发送到目标电子邮件地址
Sending EMail from my Javascript App via GMail API - mail appears in GMail sent list, but isn't delivered to destination email address
我一直在编写一个通过 REST API 与 GMail 集成的客户端(Chrome 浏览器)应用程序。我的应用程序是用 Javascript/Angular 编写的,大部分 GMail 集成工作正常。它可以从 GMail 中获取 - 电子邮件、配置文件、标签等。
我无法发送我创建的电子邮件。但是,我尝试发送的电子邮件确实出现在 GMail 已发送列表中,如果我通过添加 'INBOX' 标签修改电子邮件,它们也会出现在 GMail 收件箱中。但是 none 的电子邮件到达了目的地。我一直在测试几个电子邮件帐户——Hotmail、Yahoo 和另一个 GMail 帐户。电子邮件永远不会送达目的地 - 我检查了收件箱、垃圾邮件等。
我的代码如下...函数 'initializeGMailInterface' 首先是 运行 授权(通过用户界面),然后是 'sendEmail' 函数(也通过用户界面)。代码似乎与我见过的示例相吻合,文档 Google 提供了它们的 REST API。身份验证似乎工作正常 - 正如我提到的,我能够获取电子邮件等。
如何将电子邮件送达目的地?
var CLIENT_ID = '853643010367revnu8a5t7klsvsc5us50bgml5s99s4d.apps.googleusercontent.com';
var SCOPES = ['https://mail.google.com/', 'https://www.googleapis.com/auth/gmail.send', 'https://www.googleapis.com/auth/gmail.modify', 'https://www.googleapis.com/auth/gmail.labels'];
function handleAuthResult(authResult) {
if (authResult && !authResult.error) {
loadGmailApi();
}
}
$scope.initializeGMailInterface = function() {
gapi.auth.authorize({
client_id: CLIENT_ID,
scope: SCOPES,
immediate: true
}, handleAuthResult);
};
function loadGmailApi() {
gapi.client.load('gmail', 'v1', function() {
console.log("Loaded GMail API");
});
}
$scope.sendEmail = function() {
var content = 'HELLO';
// I have an email account on GMail. Lets call it 'theSenderEmail@gmail.com'
var sender = 'theSenderEmail@gmail.com';
// And an email account on Hotmail. Lets call it 'theReceiverEmail@gmail.com'\
// Note: I tried several 'receiver' email accounts, including one on GMail. None received the email.
var receiver = 'theReceiverEmail@hotmail.com';
var to = 'To: ' +receiver;
var from = 'From: ' +sender;
var subject = 'Subject: ' +'HELLO TEST';
var contentType = 'Content-Type: text/plain; charset=utf-8';
var mime = 'MIME-Version: 1.0';
var message = "";
message += to +"\r\n";
message += from +"\r\n";
message += subject +"\r\n";
message += contentType +"\r\n";
message += mime +"\r\n";
message += "\r\n" + content;
sendMessage(message, receiver, sender);
};
function sendMessage(message, receiver, sender) {
var headers = getClientRequestHeaders();
var path = "gmail/v1/users/me/messages?key=" + CLIENT_ID;
var base64EncodedEmail = btoa(message).replace(/\+/g, '-').replace(/\//g, '_');
gapi.client.request({
path: path,
method: "POST",
headers: headers,
body: {
'raw': base64EncodedEmail
}
}).then(function (response) {
});
}
var t = null;
function getClientRequestHeaders() {
if(!t) t = gapi.auth.getToken();
gapi.auth.setToken({token: t['access_token']});
var a = "Bearer " + t["access_token"];
return {
"Authorization": a,
"X-JavaScript-User-Agent": "Google APIs Explorer"
};
}
我一直在编写一个通过 REST API 与 GMail 集成的客户端(Chrome 浏览器)应用程序。我的应用程序是用 Javascript/Angular 编写的,大部分 GMail 集成工作正常。它可以从 GMail 中获取 - 电子邮件、配置文件、标签等。
我无法发送我创建的电子邮件。但是,我尝试发送的电子邮件确实出现在 GMail 已发送列表中,如果我通过添加 'INBOX' 标签修改电子邮件,它们也会出现在 GMail 收件箱中。但是 none 的电子邮件到达了目的地。我一直在测试几个电子邮件帐户——Hotmail、Yahoo 和另一个 GMail 帐户。电子邮件永远不会送达目的地 - 我检查了收件箱、垃圾邮件等。
我的代码如下...函数 'initializeGMailInterface' 首先是 运行 授权(通过用户界面),然后是 'sendEmail' 函数(也通过用户界面)。代码似乎与我见过的示例相吻合,文档 Google 提供了它们的 REST API。身份验证似乎工作正常 - 正如我提到的,我能够获取电子邮件等。
如何将电子邮件送达目的地?
var CLIENT_ID = '853643010367revnu8a5t7klsvsc5us50bgml5s99s4d.apps.googleusercontent.com';
var SCOPES = ['https://mail.google.com/', 'https://www.googleapis.com/auth/gmail.send', 'https://www.googleapis.com/auth/gmail.modify', 'https://www.googleapis.com/auth/gmail.labels'];
function handleAuthResult(authResult) {
if (authResult && !authResult.error) {
loadGmailApi();
}
}
$scope.initializeGMailInterface = function() {
gapi.auth.authorize({
client_id: CLIENT_ID,
scope: SCOPES,
immediate: true
}, handleAuthResult);
};
function loadGmailApi() {
gapi.client.load('gmail', 'v1', function() {
console.log("Loaded GMail API");
});
}
$scope.sendEmail = function() {
var content = 'HELLO';
// I have an email account on GMail. Lets call it 'theSenderEmail@gmail.com'
var sender = 'theSenderEmail@gmail.com';
// And an email account on Hotmail. Lets call it 'theReceiverEmail@gmail.com'\
// Note: I tried several 'receiver' email accounts, including one on GMail. None received the email.
var receiver = 'theReceiverEmail@hotmail.com';
var to = 'To: ' +receiver;
var from = 'From: ' +sender;
var subject = 'Subject: ' +'HELLO TEST';
var contentType = 'Content-Type: text/plain; charset=utf-8';
var mime = 'MIME-Version: 1.0';
var message = "";
message += to +"\r\n";
message += from +"\r\n";
message += subject +"\r\n";
message += contentType +"\r\n";
message += mime +"\r\n";
message += "\r\n" + content;
sendMessage(message, receiver, sender);
};
function sendMessage(message, receiver, sender) {
var headers = getClientRequestHeaders();
var path = "gmail/v1/users/me/messages?key=" + CLIENT_ID;
var base64EncodedEmail = btoa(message).replace(/\+/g, '-').replace(/\//g, '_');
gapi.client.request({
path: path,
method: "POST",
headers: headers,
body: {
'raw': base64EncodedEmail
}
}).then(function (response) {
});
}
var t = null;
function getClientRequestHeaders() {
if(!t) t = gapi.auth.getToken();
gapi.auth.setToken({token: t['access_token']});
var a = "Bearer " + t["access_token"];
return {
"Authorization": a,
"X-JavaScript-User-Agent": "Google APIs Explorer"
};
}