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

Windows Server 2012中smtplib.SMTP('smtp.office365.com',587)挂起求助

Troubleshooting Stuck smtplib.SMTP on Windows Server 2012 with Office 365

I’ve run into this exact issue with Windows Server 2012 and Office 365 SMTP connections before—let’s walk through the most likely fixes since you’ve already ruled out port blocking with telnet:

1. Force TLS 1.2 (Critical for Office 365 Compatibility)

Windows Server 2012 defaults to older TLS versions that Office 365 no longer supports. Even if your code tries to upgrade to TLS later, the initial handshake can hang because the server rejects outdated protocols outright.

Modify your code to explicitly enforce TLS 1.2 before creating the SMTP connection:

import smtplib
import ssl

# Create a TLS 1.2-only context
tls_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)

try:
    # Initialize SMTP connection
    server = smtplib.SMTP('smtp.office365.com', 587)
    # Use the enforced TLS context for the handshake
    server.starttls(context=tls_context)
    # Rest of your email logic (login, send message, etc.)
    server.login('your-email@domain.com', 'your-password')
    # ...
except Exception as e:
    print(f"Connection error: {str(e)}")
finally:
    server.quit()

This works because Office 365 requires TLS 1.2+ for all SMTP connections now. Windows Server 2012’s default SSL stack doesn’t prioritize this, leading to a stalled handshake.

2. Check for Proxy or WinHTTP Auto-Configuration Issues

Windows Server 2012 often runs in enterprise environments with proxies that intercept SMTP traffic—even if telnet works, Python’s smtplib might respect system proxy settings differently than the telnet client.

  • Open Internet Options > Connections > LAN Settings to check for enabled proxies or auto-configuration scripts.
  • Add smtp.office365.com to the proxy exceptions list if a proxy is active.
  • If you need to explicitly configure Python to use the system proxy, try this:
import smtplib
from urllib.request import getproxies

proxies = getproxies()
if 'https' in proxies:
    # Extract proxy host and port from system settings
    proxy_details = proxies['https'].replace('https://', '')
    server = smtplib.SMTP(proxy_details)
    server.connect('smtp.office365.com', 587)
else:
    server = smtplib.SMTP('smtp.office365.com', 587)

Note: Some enterprise proxies block SMTP traffic even with exceptions—you may need to confirm with your network admin if this is the case.

3. Update Python’s SSL Certificate Bundle

Outdated root certificates on Windows Server 2012 can cause the SSL handshake to hang, since Python uses its own certificate bundle by default.

  • Locate the cacert.pem file in your Python installation’s Lib\site-packages\certifi folder.
  • Replace this file with a fresh copy of the official CA certificate bundle (you can get this from the certifi project’s official repository—ensure you use a trusted source).
  • Alternatively, force Python to use the system’s root certificates instead:
import smtplib
import ssl

# Use Windows' built-in root certificates
system_context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
server = smtplib.SMTP('smtp.office365.com', 587)
server.starttls(context=system_context)

4. Enable Verbose Debugging to Pinpoint the Stall

Add debug logging to see exactly where the connection gets stuck. Insert this line right after initializing the SMTP object:

server.set_debuglevel(1)

This will print every step of the SMTP handshake to the console. If it stops at "connecting to server", it’s likely a protocol mismatch. If it stalls after sending the EHLO command, the server might be rejecting your server’s identity or a proxy is interfering.

5. Install Windows Server 2012 Security Updates

Older builds of Windows Server 2012 have known bugs in their TLS stack that cause hangs with modern SMTP servers. Ensure your server is fully updated with the latest security patches—many of these patches fix TLS handshake issues.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 20:07:57