为什么我总是得到一个“a”;400无效到页眉“;使用谷歌GMail游乐场时的响应?

为什么我总是得到一个“a”;400无效到页眉“;使用谷歌GMail游乐场时的响应?,gmail,gmail-api,Gmail,Gmail Api,我花了两天时间试图通过发送电子邮件,但运气不佳。以下是我试图发送的原始消息: To: "Stanley Smith" <stan.smith@yahoo.com>\r\nContent-type: text/html;charset=iso-8859-1\r\nMIME-Version: 1.0\r\nSubject: this would be the subject\r\n\r\nThis is the email sent by Stanley Smith 然后单击“发送请求

我花了两天时间试图通过发送电子邮件,但运气不佳。以下是我试图发送的原始消息:

To: "Stanley Smith" <stan.smith@yahoo.com>\r\nContent-type: text/html;charset=iso-8859-1\r\nMIME-Version: 1.0\r\nSubject: this would be the subject\r\n\r\nThis is the email sent by Stanley Smith
然后单击“发送请求”,我不断收到此错误:

HTTP/1.1 400 Bad Request
Content-length: 188
X-xss-protection: 1; mode=block
X-content-type-options: nosniff
Expires: Fri, 16 Dec 2016 15:27:27 GMT
Vary: Origin,X-Origin
Server: GSE
Cache-control: private, max-age=0
Date: Fri, 16 Dec 2016 15:27:27 GMT
X-frame-options: SAMEORIGIN
Content-type: application/json; charset=UTF-8
{
  "error": {
    "code": 400, 
    "message": "Invalid to header", 
    "errors": [
      {
        "domain": "global", 
        "message": "Invalid to header", 
        "reason": "invalidArgument"
      }
    ]
  }
}

我遵循RFC 2822,所以我不知道为什么会出现此错误。为什么会出现此错误?

我不完全确定您为什么会出现此错误。如果您重新排列标题并将其编码为base64 url安全,则此功能有效:

btoa(
  "From: \"Stanley Toles\" <stan.toles@yahoo.com>\r\n" +
  "To: \"Stanley Toles\" <stan.toles@yahoo.com>\r\n" +
  "Subject: this would be the subject\r\n" +
  "Content-type: text/html;charset=iso-8859-1\r\n\r\n" +

  "This is the email sent by Stanley Toles"
).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
btoa(
“发件人:\“Stanley Toles\”\r\n”+
“收件人:\“Stanley Toles\”\r\n”+
“主题:这将是主题\r\n”+
“内容类型:text/html;charset=iso-8859-1\r\n\r\n”+
“这是斯坦利·托尔斯发送的电子邮件”
).replace(//\+/g,'-')。replace(//\//g,'.''.')。replace(//=+$/,'');

这篇文章对我帮助很大。谢谢你问我这个问题,罗伯

最初,我在电子邮件中使用以下代码:

let email = {message: "To: firstname lastname<someperson@gmail.com>  From: firstname 
lastname <someperson@gmail.com> Subject: Saying Hello  Date: Tue, 9 Nov 2018 17:20:06 
-0600 
Message-ID: <1234@local.machine.example>  This is a message just to say hello."}
let email={message:“To:firstname lastname From:firstname”
姓氏主题:打招呼日期:2018年11月9日星期二17:20:06
-0600 
Message ID:这是一条只用来打招呼的消息。“}
读了你的帖子后,我意识到我遗漏了返回字符。以下是我的最终答案,它使用javascript中的google gmail api发送电子邮件:

function sendMessage(auth) {

    let email = {message: `To: "first last" <someperson@gmail.com>\r\nContent-type: 
    text/html;charset=iso-8859-1\r\nMIME-Version: 1.0\r\nSubject: this would be the 
    subject\r\n\r\nThis is the email sent by Stanley Toles`}

    let base64EncodedEmail = Base64.encodeURI(email.message);

    gmail.users.messages.send({'auth': auth, 'userId': 'me',  'resource': {
     "payload": {
      "headers": [
       {name: "To", value: "first last <somepersonsemail@gmail.com>"},
       {name: 'From', value: 'first last <somepersonsemail@gmail.com>'},
       {name: 'Subject', value: 'Saying Hello'}]
    },
    'raw': base64EncodedEmail,


    }},function(err,response){

    if(err) throw err;

    console.log(response);
   });
  }
函数发送消息(auth){
让电子邮件={message:`To:“first-last”\r\n内容类型:
text/html;charset=iso-8859-1\r\n时间版本:1.0\r\n对象:这将是
主题\r\n\r\n这是Stanley Toles发送的电子邮件`}
让base64encodemail=Base64.encodeURI(email.message);
gmail.users.messages.send({'auth':auth,'userId':'me','resource':{
“有效载荷”:{
“标题”:[
{name:“To”,value:“first last”},
{name:'From',value:'first last'},
{name:'Subject',value:'Saying Hello'}]
},
“原始”:base64EncodedEmail,
}},函数(错误,响应){
如果(错误)抛出错误;
控制台日志(响应);
});
}

尝试首先使用诸如from和Subject之类的标题构建消息,然后在发送消息之前对消息进行编码。然后按照。对你有用吗?@noogui这与实际的API无关。我只是想利用这个游乐场@Tholle,这很有效!谢谢如果你把这个作为答复,我会接受的。
function sendMessage(auth) {

    let email = {message: `To: "first last" <someperson@gmail.com>\r\nContent-type: 
    text/html;charset=iso-8859-1\r\nMIME-Version: 1.0\r\nSubject: this would be the 
    subject\r\n\r\nThis is the email sent by Stanley Toles`}

    let base64EncodedEmail = Base64.encodeURI(email.message);

    gmail.users.messages.send({'auth': auth, 'userId': 'me',  'resource': {
     "payload": {
      "headers": [
       {name: "To", value: "first last <somepersonsemail@gmail.com>"},
       {name: 'From', value: 'first last <somepersonsemail@gmail.com>'},
       {name: 'Subject', value: 'Saying Hello'}]
    },
    'raw': base64EncodedEmail,


    }},function(err,response){

    if(err) throw err;

    console.log(response);
   });
  }