【问题标题】:Python socket.gaierror: [Errno 11001] getaddrinfo failedPython socket.gaierror:[Errno 11001] getaddrinfo 失败
【发布时间】:2021-10-05 03:34:19
【问题描述】:

尝试在 python 中创建邮件发件人,该脚本可以在我的个人笔记本电脑上运行,但是当我在我的工作笔记本电脑上运行它时(我是最近的实习生),我认为代理妨碍了与gmail smtp 服务器

错误如下:

File "D:\ocm-hours-report-automation\mail-manager\src\python\mail-sender.py", line 44, in <module>
    session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 341, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 312, in _get_socket
    return socket.create_connection((host, port), timeout,
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python39\lib\socket.py", line 822, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python39\lib\socket.py", line 953, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

代码是:

#The mail addresses and password
sender_address = 'email1@gmail.com'
sender_pass = 'password'
receiver_address = 'email2@gmail.com'
#Setup the MIME
message = MIMEMultipart()
message['From'] = sender_address
message['To'] = receiver_address
message['Subject'] = 'A test mail sent by Python. It has an attachment.'   #The subject line
#The body and the attachments for the mail
message.attach(MIMEText(mail_content, 'plain'))
#Create SMTP session for sending the mail
session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
session.starttls() #enable security
session.login(sender_address, sender_pass) #login with mail_id and password
text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
session.quit()
print('Mail Sent')

任何想法我实际上可以做什么?尝试强制 socks.setdefaultproxy 但它说 socks 导入不可用

谢谢!

【问题讨论】:

    标签: python email proxy smtp socks


    【解决方案1】:

    你的怀疑是正确的。 socket.gaierror: [Errno 11001] getaddrinfo failed 表示应用程序无法解析主机的 IP 地址。有多种方法可以解决这个问题,具体取决于运行代码的机器的操作系统,例如在 /etc/hosts 中手动映射。但是,即使 IP 地址被解析,也不意味着代理会允许连接到它。

    【讨论】:

    • 我没有使用linux tho
    • 但我决定不想使用 gmail smtp 执行此操作,因为我们公司提供内部 smtp。它是这样工作的
    • /etc/hosts 不是 Linux 独有的。在 Windows 上:C:\Windows\System32\drivers\etc\hosts。很高兴您找到了可行的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    • 2018-02-12
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    相关资源
    最近更新 更多