Python SMTP - [Errno 101] 网络在 AWS EC2 上无法访问

Python SMTP - [Errno 101] Network is unreachable on AWS EC2

我正在尝试验证用户的电子邮件地址。

尝试使用 MX 记录连接到用户的主机时,我总是在 AWS EC2 实例上收到 'Network is unreachable' 错误。但是,它在本地工作得很好。

records = dns.resolver.query(domain, 'MX')
mx_record = records[0].exchange
mx = str(mx_record)
smtp_server = smtplib.SMTP(host=mx)

如果我尝试使用:

smtp_server = smtplib.SMTP_SSL(host=mx, port=465)

我遇到了同样的问题。

请分享任何想法。谢谢!

可能不起作用because:

AWS blocks outbound traffic on port 25 (SMTP) of all EC2 instances and Lambda functions by default. If you want to send outbound traffic on port 25, you can request for this restriction to be removed.

虽然没有明确说明,但我会说 465 也是一样的。

如 link 中所述,您必须联系 AWS 支持以取消此限制,或使用 AWS SES.