You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Gmail API发送邮件时持续出现400错误:Recipient address required

Fixing "Recipient address required" Error in Gmail API (JavaScript & curl)

Let's break down why you're hitting this 400 error and fix it step by step:

Core Issues Identified

  1. Mismatched Content-Type and Request Body
    You set Content-Type: message/rfc822 but sent a JSON body with the raw field. These two formats are incompatible—Gmail API can't parse the recipient info when this mismatch happens.

  2. Missing Blank Line Between Headers and Body
    Your raw email content doesn't have a blank line after the Subject header. RFC 822 requires this separator to distinguish headers from the message body; without it, the API may misinterpret the entire content as headers, failing to find valid recipient data.

  3. Unnecessary Quotes Around Names
    Wrapping names in single quotes (e.g., 'John Doe') isn't required for email headers and can cause parsing issues. Stick to the standard format: Name <email@domain.com>.


Solution 1: Use application/json with URL-Safe Base64 raw Field

This is the most common approach for JSON-based requests:

  • Set Content-Type: application/json
  • Encode your properly formatted email as URL-safe Base64 (replace + with -, / with _, and strip trailing = padding)
  • Ensure the email has the mandatory blank line between headers and body

Example curl Command

curl -X POST "https://gmail.googleapis.com/gmail/v1/users/john.doe@gmail.com/messages/send" \
  -H "Authorization: Bearer ya..." \
  -H "Content-Type: application/json" \
  -d '{
    "raw": "RnJvbTogSm9obiBEb2UgPGpvaG4uZG9lQGdtYWlsLmNvbT4KVG86IEphbmUgRG9lIDxqYW5lLmRvZUBnbWFpbC5jb20+ClN1YmplY3Q6IFRoaXMgaXMgYSBzdWJqZWN0CgpEaWQgeW91IHJlY2VpdmUgdGhpcyBtZXNzYWdlPw=="
  }'

Example JavaScript Code

// Properly formatted email string with blank line separator
const emailContent = `From: John Doe <john.doe@gmail.com>
To: Jane Doe <jane.doe@gmail.com>
Subject: This is a subject

Did you receive this message?`;

// Convert to URL-safe Base64
const raw = btoa(emailContent)
  .replace(/\+/g, '-')
  .replace(/\//g, '_')
  .replace(/=+$/, '');

// Send request with correct Content-Type
fetch('https://gmail.googleapis.com/gmail/v1/users/john.doe@gmail.com/messages/send', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer ya...',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ raw })
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));

Solution 2: Use message/rfc822 with Raw Email Content

If you prefer sending the raw email directly (no JSON wrapper):

  • Set Content-Type: message/rfc822
  • Send the plain text email content as the request body (make sure headers and body are separated by a blank line)

Example curl Command

curl -X POST "https://gmail.googleapis.com/gmail/v1/users/john.doe@gmail.com/messages/send" \
  -H "Authorization: Bearer ya..." \
  -H "Content-Type: message/rfc822" \
  -d $'From: John Doe <john.doe@gmail.com>
To: Jane Doe <jane.doe@gmail.com>
Subject: This is a subject

Did you receive this message?'

Key Checks to Verify

  • Double-check that your access token has the https://www.googleapis.com/auth/gmail.send scope
  • Ensure the email headers (From, To) use valid email addresses associated with your authenticated account
  • Confirm the Base64 encoding is URL-safe (critical for the raw field method)

内容的提问来源于stack exchange,提问作者Fabio Lama

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 09:52:51