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

搭建无需登录的SMTP服务器实现Python错误邮件告警时遇Socket错误

Troubleshooting Your Python Email Alert Script on Windows Server 2012

Let's break down your problem step by step—there are a few key issues here that are likely causing your vague error, and port blocking might not even be the main culprit.

First: Your Local SMTP Setup Is Misconfigured

Your code tries to use smtpd.SMTPServer alongside smtplib.SMTP, but that's a fundamental mismatch:

  • smtpd.SMTPServer is a mail-receiving server (it listens for incoming emails, not sends them). It can't handle outgoing send requests from smtplib.
  • Using port 80 is also odd—SMTP's standard port is 25, and port 80 is reserved for HTTP traffic. Even if you got the server running, most systems block non-HTTP traffic on port 80 by default.

This backwards setup is almost certainly causing your generic ('Unexpected error:', ) message.

Second: Understanding netstat Output (So You Can Stop Guessing Ports)

When you run netstat -an | find /i "25", here's how to interpret the results without confusion:

  • Look for lines with LISTENING: This means a service is actively waiting for connections on port 25. If you don't see this, no SMTP server is running locally on that port.
  • Lines with ESTABLISHED or TIME_WAIT are existing or closed connections—they don't tell you if the port is available for listening.

To narrow it down to only listening ports on 25, run this more precise command:

netstat -an | findstr /R /C:"LISTENING.*:25"

If you get no output, that confirms no service is listening on port 25 (which makes sense, since your smtpd setup isn't working as a sendable server).

Fixes to Get Email Alerts Working

Since you don't want to use an external SMTP server requiring login, the easiest and most reliable solution is to use Windows Server's built-in SMTP service. Here's how:

1. Enable Windows Built-in SMTP Server

  1. Open Server Manager → Go to Add Roles and Features.
  2. Under Features, check SMTP Server (you may need to add related management tools too).
  3. After installation, open Internet Information Services (IIS) 6.0 Manager.
  4. Navigate to your SMTP server, right-click → Properties.
  5. Under the Access tab:
    • Click Connection → Allow only Local computer (to keep it secure).
    • Click Relay → Allow only Local computer (prevents your server from being used as an open relay).

2. Update Your Python Code to Use the Windows SMTP Server

Simplify your email code—you don't need smtpd or threading anymore. Replace your existing SMTP setup with this:

# Replace your current SMTP code block with this
fromAddr = 'robert.domiano@spireenergy.com'
toAddr = 'robert.domiano@spireenergy.com'
text = 'This is a test of sending email from within python.'

try:
    # Connect to Windows' local SMTP server on port 25
    server = smtplib.SMTP('localhost', 25)
    # No need for starttls() since it's local and unencrypted by default
    server.sendmail(fromAddr, toAddr, text)
    server.quit()
    print("Email sent successfully!")
except Exception as e:
    print(f"Failed to send email: {str(e)}")
    traceback.print_exc()

3. Fix Your Error Handling to Get Useful Details

Your current except block only prints the error type, not the actual message. Update it to show full traceback so you can debug precisely:

except Exception as e:
    print("Unexpected error occurred:")
    print(str(e))
    traceback.print_exc()
    # Rest of your error handling code here

This will tell you exactly if the issue is a connection refusal, authentication problem, or something else.

Verifying Port Blocking (If You Still Suspect It)

If you're still worried about port blocking:

  • A "Connection refused" error means no service is listening on the port (not blocked).
  • A timeout error means the port is blocked by a firewall.
  • To test port 25 locally, use telnet localhost 25—if it connects, the port is open and listening. If it fails, either no service is running or the firewall is blocking it.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 17:38:10