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

Windows下Python3.9.10调用Gmail SMTP发邮件报socket.gaierror错误

Windows下Python调用Google SMTP发送邮件报错socket.gaierror: [Errno 11001] getaddrinfo failed

在Windows系统使用Python 3.9.10调用Google SMTP发送邮件时遇到问题,代码如下:

import smtplib
from email.mime.text import MIMEText

def send_email(host, port, subject, msg, sender, recipients, password):
    msg = MIMEText(msg)
    msg['Subject'] = subject
    msg['From'] = sender
    msg['To'] = ', '.join(recipients)
    smtp_server = smtplib.SMTP_SSL(host, port)
    smtp_server.login(sender, password)
    smtp_server.sendmail(sender, recipients, msg.as_string())
    smtp_server.quit()


def main():
    host = "smtp.gmail.com"
    port = 465

    user = <my email>
    pwd = <google app password>

    subject = "Test email"
    msg = "Hello world"
    sender = user
    recipients = [user]
    send_email(host, port, subject, msg, sender, recipients, pwd)


if __name__ == '__main__':
    main()

持续报错socket.gaierror: [Errno 11001] getaddrinfo failed,报错堆栈信息如下:

Traceback (most recent call last):
  File "%project_location%\send_email_google.py", line 37, in <module>
    main()
  File "%project_location%\send_email_google.py", line 33, in main
    send_email(host, port, subject, msg, sender, recipients, pwd)
  File "%project_location%\send_email_google.py", line 13, in send_email
    smtp_server = smtplib.SMTP_SSL(host, port)
  File "%Python%\lib\smtplib.py", line 1050, in __init__
    SMTP.__init__(self, host, port, local_hostname, timeout,
  File "%Python%\lib\smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
  File "%Python%\lib\smtplib.py", line 341, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "%Python%\lib\smtplib.py", line 1056, in _get_socket
    new_socket = super()._get_socket(host, port, timeout)
  File "%Python%\lib\smtplib.py", line 312, in _get_socket
    return socket.create_connection((host, port), timeout,
  File "%Python%\lib\socket.py", line 823, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "%Python%\lib\socket.py", line 954, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

Process finished with exit code 1

未使用VPN或代理,已确认Gmail的主机和端口正确,也使用了Google应用密码,不清楚问题所在,希望得到帮助。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 03:40:20