使用 javascript 通过 google api 发送邮件失败 [英] Failed sending mail through google api with javascript

查看:21
本文介绍了使用 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);
});

这篇关于使用 javascript 通过 google api 发送邮件失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆