使用Yandex SMTP发邮件遇ConnectionRefusedError[WinError10061]求助
Hey, let's work through this Yandex SMTP connection issue together. I've dealt with similar problems before, so here's a step-by-step breakdown to fix it, plus some solid alternatives if you still hit roadblocks.
1. Double-Check Yandex Account & SMTP Configuration
The most common culprit here is incorrect account permissions or using the wrong password. Yandex has strict security rules for third-party apps:
- Log into your Yandex account, go to Account Settings (avatar → Account Management)
- Navigate to the Security section and enable Two-Step Verification if you haven’t already
- After enabling two-step verification, generate an App-Specific Password — this is what you need to use for the
PASSWORDvariable in your code, not your regular email login password - Confirm your SMTP settings are correct:
smtp.yandex.ru(host),587(port), and TLS encryption is mandatory
2. Rule Out Local Network Restrictions
Since you suspect your local PC might be the issue, try these checks:
- Temporarily disable Windows Firewall or add your Python executable to the allowed apps list — firewalls often block outbound SMTP connections
- If you’re using a proxy server, disable it temporarily to test if it’s interfering with the connection
- Test connectivity directly with telnet: Open Command Prompt and run
telnet smtp.yandex.ru 587. If this fails, your network is blocking the connection; if it works, the issue is in your code/account setup
3. Fix Your Python Code
Your code has a couple of small gaps that might be contributing. Here’s a revised, error-handled version:
import pandas as pd import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText # Replace with your valid credentials HOST = 'smtp.yandex.ru' PORT = 587 SUBJECT = 'accuracy score' TO = 'your-real-email@example.com' FROM = 'your-yandex-email@yandex.ru' PASSWORD = 'your-app-specific-password' # Use the generated app password here # Ensure these variables are defined with your actual data period = "2024-01-01 to 2024-01-31" true_peaks = 0.95 false_peaks = 0.12 msg = MIMEMultipart() msg['From'] = FROM msg['To'] = TO msg['Subject'] = SUBJECT message = 'Period: %s with true peaks accuracy = %s, and false peaks accuracy = %s' % (period, true_peaks, false_peaks) msg.attach(MIMEText(message, 'plain')) try: server = smtplib.SMTP(HOST, PORT) server.ehlo() server.starttls() server.ehlo() # Some servers require a second EHLO after TLS initiation server.login(FROM, PASSWORD) server.sendmail(FROM, TO, msg.as_string()) print("Email sent successfully!") except smtplib.SMTPException as e: print(f"SMTP error occurred: {str(e)}") except ConnectionRefusedError as e: print(f"Connection refused: {str(e)}") finally: server.quit()
If Yandex still gives you trouble, try these reliable alternatives:
1. Outlook/Hotmail SMTP
Microsoft’s SMTP service is stable and easy to configure:
- Host:
smtp.office365.com - Port: 587
- Enable two-step verification on your Microsoft account, then generate an app password for your script
2. SendGrid (Free Tier Available)
SendGrid is designed for automated emails, with a free tier that lets you send 100 emails per month. Here’s a quick example:
First install the library:
pip install sendgrid
Then use this code:
from sendgrid import SendGridAPIClient from sendgrid.helpers.mail import Mail message = Mail( from_email='your-email@example.com', to_emails='recipient@example.com', subject='accuracy score', plain_text_content='Period: 2024-01-01 to 2024-01-31 with true peaks accuracy = 0.95, and false peaks accuracy = 0.12') try: sg = SendGridAPIClient('your-sendgrid-api-key') response = sg.send(message) print(f"Email sent! Status code: {response.status_code}") except Exception as e: print(f"Error: {str(e)}")
3. Local SMTP Server
If your network has an internal SMTP server, you can configure your script to use it directly — just update the HOST and PORT variables to match your network’s server details (no password needed if it’s unauthenticated).
内容的提问来源于stack exchange,提问作者Lumos

