Python SMTP发送邮件至uc.cl域名失败问题排查求助
Let’s walk through how to figure out why your emails to @uc.cl aren’t getting delivered, step by step:
1. First, rule out small code issues and get visibility into errors
Your code looks mostly correct, but there are a few tweaks and checks you can do to eliminate code-related problems:
- Add error handling: Right now, your code doesn’t catch any exceptions, so you might not even know if the email is failing to send from Gmail’s end. Wrap the sendmail call in a try/except block to see what’s happening:
try: mailserver.sendmail('example1@gmail.com','example2@uc.cl',msg.as_string()) print("Email sent successfully from Gmail!") except Exception as e: print(f"Failed to send: {str(e)}") - Specify text encoding: Your message uses Spanish characters, so explicitly setting UTF-8 encoding for the MIMEText part can prevent garbled content that might trigger spam filters:
msg.attach(MIMEText(message, 'plain', 'utf-8')) - Confirm Gmail authentication: If you’re using a regular password, note that Gmail now requires App Passwords if you have 2FA enabled (and "less secure apps" access is no longer available). Make sure you’re using the correct App Password instead of your regular account password if 2FA is on.
2. Check UC.cl’s email security policies
Universities often have strict anti-spam and filtering rules, which is likely the culprit here:
- Ask the recipient to check spam/archive folders: First, have the person at @uc.cl look in their spam, junk, or archived mail—many legitimate emails get misclassified automatically.
- Test with neutral content: Your current message ("Wena mimo, como estai mimo") is very casual and might trigger spam detectors. Try sending a more formal, generic message like:
Testing email delivery to UC.cl domain. Please let me know if you receive this.
- Verify Gmail’s deliverability for UC.cl: Gmail’s own SPF/DKIM records are solid, but some university domains block or throttle emails from large providers like Gmail. You can test sending to a different @uc.cl address to see if the issue is specific to one mailbox or the entire domain.
3. Try alternative port/encryption settings
While port 587 with STARTTLS is standard for Gmail, switching to port 465 with SSL might bypass any filtering issues related to STARTTLS handshakes:
# Replace the SMTP setup section with this mailserver = smtplib.SMTP_SSL('smtp.gmail.com', 465) mailserver.login('example1@gmail.com', 'your-app-password') # No need for ehlo() or starttls() when using SMTP_SSL
4. Verify delivery status from Gmail’s side
- Check your "Sent" folder: If the email shows up in your Gmail Sent folder, that means Gmail successfully passed it to its outgoing servers—so the problem is almost certainly with UC.cl’s incoming filters.
- Inspect email headers: Open the sent email in Gmail, click the three dots > "Show original" to view the full headers. Look for any lines indicating bounces, delays, or rejected messages from the UC.cl server (look for entries with
uc.clin them).
内容的提问来源于stack exchange,提问作者gañaufla
相关产品推荐
相关产品推荐

