"Recipient address required" 即使在对原始消息进行编码之后

"Recipient address required" even after encoding the raw message

我在通过 Gmail API 发送电子邮件时遇到问题。我遵循了各种来源指定的说明:

留言:

From: 'John Doe' <john.doe@gmail.com>\n
To: 'Jane Doe' <jane.doe@gmail.com>\n
Subject: This is a subject\n
Did you receive this message?

(有和没有'我都试过了,包括直接指定电子邮件地址)

我将其编码为 base64(url 安全)并将其添加到 body 的“原始”字段中。 请注意,我将完整的 JSON 作为 body 传递。

{
   "raw":"RnJvbTogJ0pvaG4gRG9lJyA8am9obi5kb2VAZ21haWwuY29tPgpUbzogJ0phbmUgRG9lJyA8amFuZS5kb2VAZ21haWwuY29tPgpTdWJqZWN0OiBUaGlzIGlzIGEgc3ViamVjdApEaWQgeW91IHJlY2VpdmUgdGhpcyBtZXNzYWdlPw=="
}

(有无填充我都试过了)

我正在使用以下 HTTP headers:

   "headers":{
      "content-type":"message/rfc822",
      "authorization":"Bearer ya..."
   }

https://gmail.googleapis.com/upload/gmail/v1/users/john.doe@gmail.com/messages/send 发出 POST 请求后,我得到以下响应:

{
  "error": {
    "code": 400,
    "message": "Recipient address required",
    "errors": [
      {
        "message": "Recipient address required",
        "domain": "global",
        "reason": "invalidArgument"
      }
    ],
    "status": "INVALID_ARGUMENT"
  }
}

编辑: 官方 Google 文档对于语言中立性并没有多大帮助。

事实证明,you cannot send email with a service account。官方 Google 支持页面直接告诉您使用第三方服务:

https://cloud.google.com/compute/docs/tutorials/sending-mail/

所以我刚 old-school 使用 SMTP。效果很好。