简体   繁体   English

socket.gaierror:[Errno 11001] getaddrinfo 失败

[英]socket.gaierror: [Errno 11001] getaddrinfo failed

I have tried to attached a file to the mail using python.我尝试使用 python 将文件附加到邮件中。 Code:代码:

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from smtplib import SMTPException


def send_Email():
    file1="abc.txt"
    message = "Test mail"
    msg = MIMEMultipart()

    msg.attach(MIMEText(file(file1).read()))

    try:
        smtpObj = smtplib.SMTP('smtp server name',port)
        smtpObj.sendmail(sender, EmailId, message, msg.as_string() )
        print "Successfully sent email"
    except SMTPException:
        print "Error: unable to send email"

Bt I have get the error: socket.gaierror: [Errno 11001] getaddrinfo failed Bt 我得到了错误:socket.gaierror: [Errno 11001] getaddrinfo failed

full error message:完整的错误信息:

 File "C:\Python27\lib\smtplib.py", line 249, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Python27\lib\smtplib.py", line 309, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Python27\lib\smtplib.py", line 284, in _get_socket
    return socket.create_connection((port, host), timeout)
  File "C:\Python27\lib\socket.py", line 553, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11001] getaddrinfo failed

我确信当您从代理后面工作时会出现 gaierror。

问题是“smtp 服务器名称”的 DNS 查找失败 - 如果这是您的确切代码,那么您可以看到原因 - 如果不是,并且您拥有 SMTP 服务器的有效限定名称,那么您可能遇到防火墙问题/互联网连接等,还必须将端口设置为有效值以匹配您的服务器 SMTP 配置(通常是端口 25,并非绝对总是如此)。

In my case was a host problem.就我而言,是主机问题。 Using debug mode, I spotted that in (host, port, 0, SOCK_STREAM) I got host=local and it should be host=localhost .使用调试模式,我发现在 (host, port, 0, SOCK_STREAM) 我得到了host=local ,它应该是host=localhost In the run.py I defined localhost and the file hosts (c:\\windows\\system32\\drivers\\etc\\hosts) was defined local .在 run.py 中,我定义了localhost并且文件 hosts (c:\\windows\\system32\\drivers\\etc\\hosts) 被定义为local They have to be equal, otherwise you get the socket.gaieeror.它们必须相等,否则你会得到 socket.gaieeror。

There seems to be a bug in urllib3 version 1.25.9 package. urllib3 版本 1.25.9 包中似乎存在一个错误。 This produced "socket.gaierror: [Errno 11001] getaddrinfo failed" error for me (working from behind an authenticated proxy server).这对我产生了“socket.gaierror: [Errno 11001] getaddrinfo failed”错误(在经过身份验证的代理服务器后面工作)。 Downgrading to urllib3 version 1.25.8 solved the problem.降级到 urllib3 版本 1.25.8 解决了这个问题。

The below answer may be quite irrelevant to the question.以下答案可能与问题无关。 But,some users may have a different scenario.但是,一些用户可能有不同的场景。

If a server can be reached only through VPN and if we try to reach it with VPN disconnected, this error : "gaierror: [Errno 11001] getaddrinfo failed" crops up.如果只能通过 VPN 访问服务器,并且我们尝试在 VPN 断开连接的情况下访问它,则会出现此错误:“gaierror: [Errno 11001] getaddrinfo failed”。

Connect to VPN and then executing the code should work good.连接到 VPN 然后执行代码应该可以正常工作。

you might did a little mistake in settings.py file.. check your code one more time in your settings file settings.py:你可能在 settings.py 文件中犯了一个小错误..在你的设置文件 settings.py 中再次检查你的代码:

EMAIL_BACKEND='django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your_email'
EMAIL_HOST_PASSWORD = 'your_password'
EMAIL_PORT = 587
EMAIL_USE_TLS=True

I got this error when I tried using flask-mail I just had to resend the message and it worked perfectly well.当我尝试使用瓶邮件时,我遇到了这个错误,我只需要重新发出消息,并且效果很好。 I don't know why I got the error the first time perhaps it might be a bug in the library...我不知道为什么我第一次遇到错误,也许这可能是库中的错误...

You need to login using your credential.您需要使用您的凭据登录。 Try:尝试:

 smtpObj = smtplib.SMTP('smtp server name',port)

 smtpObj .starttls() 
 smtpObj .login(email, password)
 smtpObj.sendmail(sender, EmailId, message, msg.as_string() )
 print "Successfully sent email"

I prefer u guys to run the file as administrator for eg open cmd as administrator then type cd C:\\into ur .py file path and then type python filename.py我更喜欢你们以管理员身份运行文件,例如以管理员身份打开 cmd 然后输入cd C:\\into ur .py 文件路径,然后输入python filename.py

it worked for me.它对我有用。 good luck祝你好运

您需要为您的主机邮件激活 IMAP/SMTP 服务。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (Python)socket.gaierror:[Errno 11001] getaddrinfo失败 - (Python) socket.gaierror: [Errno 11001] getaddrinfo failed Scapy导入错误'socket.gaierror:[Errno 11001] getaddrinfo失败' - Scapy import error 'socket.gaierror: [Errno 11001] getaddrinfo failed' 错误:- socket.gaierror:[Errno 11001] getaddrinfo 在获取请求中失败 - Error :- socket.gaierror: [Errno 11001] getaddrinfo failed in get request 如何解决:socket.gaierror: [Errno 11001] getaddrinfo failed - How to solve: socket.gaierror: [Errno 11001] getaddrinfo failed socket.gaierror: [Errno 11001] getaddrinfo 在 django 中失败 - socket.gaierror: [Errno 11001] getaddrinfo failed in django gaierror socket.gaierror: [Errno 11001] getaddrinfo 失败 mail.send(msg) - gaierror socket.gaierror: [Errno 11001] getaddrinfo failed mail.send(msg) 当我在 python 中使用套接字模块时,我收到此错误:'socket.gaierror: [Errno 11001] getaddrinfo failed' - I get this error:' socket.gaierror: [Errno 11001] getaddrinfo failed' when I use the socket module in python socket.gaierror:[Errno 11003] getaddrinfo失败 - socket.gaierror: [Errno 11003] getaddrinfo failed socket.gaierror: [Errno 11001] getaddrinfo failed" in python,同时使用简单的自定义 web 浏览器 - socket.gaierror: [Errno 11001] getaddrinfo failed" in python, while using a simple custom web browser Flask-Mail 显示 socket.gaierror: [Errno 11001] getaddrinfo failed - Flask-Mail showing socket.gaierror: [Errno 11001] getaddrinfo failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM