Failed sending mail through google api with javascript

隐身守侯 提交于 2019-11-26 14:35:14

问题


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 payload message string or uploading message via /upload/* URL required`

My code

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\"\n" +
          "Content-length: 5000\n" +
          "Content-Transfer-Encoding: message/rfc2822\n" +
          "to: someone@someone.nl\n" +
          "from: \"test\" <test@gmail.com>\n" +
          "subject: Hello world\n\n" +

          "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);
   });
  });        

}

回答1:


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);
});


来源:https://stackoverflow.com/questions/30590988/failed-sending-mail-through-google-api-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!