如何通过Gmail API发送电子邮件(Javascript)?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (506)

我正试图通过JS Gmail API发送一个hello world发送邮件,使用以下代码,在浏览器中运行:

const message =
"From: my.email@gmail.com\r\n" + 
"To: my.email@gmail.com\r\n" +
"Subject: As basic as it gets\r\n\r\n" +
"This is the plain text body of the message.  Note the blank line between the header information and the body of the message.";


// The body needs to be base64url encoded.
const encodedMessage = btoa(message)

const reallyEncodedMessage = encodedMessage.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')

gapi.client.gmail.users.messages.send({
    userId: 'me',
    requestBody: {
        // same response with any of these
        raw: reallyEncodedMessage
        // raw: encodedMessage
        // raw: message
    }
}).then(function () { console.log("done!")});

给出了一个HTTP 400响应:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "invalidArgument",
    "message": "'raw' RFC822 payload message string or uploading message via /upload/* URL required"
   }
  ],
  "code": 400,
  "message": "'raw' RFC822 payload message string or uploading message via /upload/* URL required"
 }
}

是哪出错了?

提问于
用户回答回答于

应该这样修改:

将以下代码:

requestBody: {
    // same response with any of these
    raw: reallyEncodedMessage
    // raw: encodedMessage
    // raw: message
}

改为:

resource: { // Modified
    // same response with any of these
    raw: reallyEncodedMessage
    // raw: encodedMessage
    // raw: message
}

扫码关注云+社区

领取腾讯云代金券

玩转腾讯云 有奖征文活动