使用Python通过Gmail委托账户发送邮件失败求助
Hey there, let's work through this frustrating issue where your Python script sends emails fine from your personal Gmail account, but fails when using a delegated account. I know it's extra annoying when Google searches don't turn up clear fixes—let's break down the most likely culprits and fixes step by step.
First: Double-Check Your Gmail Delegation Settings
From your delegation screenshot, make sure you've covered these critical steps (it's easy to miss a detail here):
- Confirm the delegated account has been granted "Send mail as" permission (not just inbox access). In Gmail settings, go to "Accounts and Import" > "Send mail as"—you should see the delegated email listed here, with a confirmation that it's been verified.
- Ensure the delegation was set up correctly: the owner of the delegated account must explicitly grant you permission to send on their behalf, not just the other way around.
- Wait a bit! Google's permission sync can sometimes take 15-30 minutes to fully kick in—if you just set up the delegation, give it some time before testing again.
Next: Audit Your Python Script's SMTP Configuration
Looking at your script screenshot, here are the key things to verify:
- Your
Fromheader must be the delegated account's email address, not your personal one. For example:msg['From'] = "delegated-account@yourdomain.com" - You should be logging into SMTP with your personal account's credentials (since you're the one authorized to send via the delegated account). If you have 2FA enabled on your personal account, do NOT use your regular password—you need to create and use an App Password (this is non-negotiable for modern Gmail SMTP logins).
- Confirm your SMTP server settings are correct:
- Server:
smtp.gmail.com - Port: 587 (with TLS) or 465 (with SSL)
- Example working snippet (adjust to match your script):
import smtplib from email.mime.text import MIMEText # Compose the email message = MIMEText("Your monthly scorecard content here") message['Subject'] = "Monthly Performance Scorecard" message['From'] = "delegated-account@yourdomain.com" message['To'] = "recipient@example.com" # Send via SMTP with smtplib.SMTP('smtp.gmail.com', 587) as server: server.starttls() # Log in with YOUR personal account and its App Password server.login("your-personal-account@gmail.com", "your-app-password-123") server.send_message(message)
- Server:
Addressing the Error Message (From Your Screenshot)
Since you mentioned Google searches didn't help with the error, let's cover common delegation-specific errors:
- If you're seeing a
550-5.7.1 Unauthorizederror: This almost always means the delegated account hasn't properly granted you send-as permissions, or your script'sFromheader doesn't match the delegated email exactly. - If you get a
535-5.7.8 Username and Password not acceptederror: Double-check that you're using an App Password (not your regular password) if 2FA is enabled, and that your personal account has permission to send via the delegated address. - If you see a
421-4.7.0 Too many concurrent connectionserror: Add a small delay between test sends, or check if your script is creating multiple SMTP connections unnecessarily.
Test the Delegation in Web Gmail First
Before digging deeper into code, log into your personal Gmail account via the web, then click the "From" dropdown when composing an email and select the delegated account. Send a test email to yourself—if this fails, the problem is with your Gmail delegation setup, not your Python script. If it works, then the issue is definitely in your script's configuration.
If All Else Fails: Try OAuth2 Authentication
Gmail is tightening restrictions on password-based SMTP logins, especially for delegated accounts. Switching to OAuth2 can resolve permission-related issues. Here's a quick overview:
- Create a project in the Google Cloud Console, enable the Gmail API, and create OAuth credentials.
- Use the
google-authandgoogle-auth-oauthliblibraries to authenticate your script, then send emails via SMTP with the OAuth2 token.
This method is more secure and less likely to trigger Gmail's security blocks for delegated accounts.
内容的提问来源于stack exchange,提问作者Naveen Chebrolu

