使用Gmail API发送邮件时持续出现400错误:Recipient address required
Let's break down why you're hitting this 400 error and fix it step by step:
Core Issues Identified
Mismatched Content-Type and Request Body
You setContent-Type: message/rfc822but sent a JSON body with therawfield. These two formats are incompatible—Gmail API can't parse the recipient info when this mismatch happens.Missing Blank Line Between Headers and Body
Your raw email content doesn't have a blank line after theSubjectheader. 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.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.sendscope - 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
rawfield method)
内容的提问来源于stack exchange,提问作者Fabio Lama

