使用 javascript 通过 google api 发送邮件失败

问题描述:

我正在尝试使用 JavaScript 通过 Google API 发送电子邮件.

I'm trying to send an email through Google API with JavaScript.

我的问题是,当我尝试发送不带附件的简单邮件时,出现以下错误:

My issue is that when I try to send a simple mail with no attachments, I get the following error:

'raw' RFC822 有效负载消息字符串或通过/upload/* URL 需要上传消息`

'raw' RFC822 payload message string or uploading message via /upload/* URL required`

我的代码

function sendMessage() {
gapi.client.load('gmail', 'v1', function() {
    // Web-safe base64 
    var to = 'someone@someone.nl',
        subject = 'Hello World',
        content = 'send a Gmail.'

    var base64EncodedEmail = btoa(
          "Content-Type:  text/plain; charset="UTF-8"
" +
          "Content-length: 5000
" +
          "Content-Transfer-Encoding: message/rfc2822
" +
          "to: someone@someone.nl
" +
          "from: "test" <test@gmail.com>
" +
          "subject: Hello world

" +

          "The actual message text goes here"
            ).replace(/+/g, '-').replace(///g, '_');

    var mail= base64EncodedEmail;
    console.log(mail);
    var request = gapi.client.gmail.users.messages.send({
      'userId': "me",
      'message': {
          'raw': mail
        }
    });
    request.execute(function(response){
     console.log(response);
   });
  });        

}

几天后我自己找到了答案.问题是只有当您在电子邮件中发送附件时才能使用正文中的消息".

After days i had found the answer by my own. The problem was that the 'message' in the body only can be used when you send an attachment in the email.

如果您没有附件,查询看起来就像我在这里写的

If you have no attachment the query looks like I wrote down here

var mail= base64EncodedEmail;
console.log(mail);
var request = gapi.client.gmail.users.messages.send({
  'userId': "me",
  'resource': {
      'raw': mail
    }
});
request.execute(function(response){
 console.log(response);
});