简体   繁体   中英

socket.gaierror: [Errno 11001] getaddrinfo failed

I have tried to attached a file to the mail using 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

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 . In the run.py I defined localhost and the file hosts (c:\\windows\\system32\\drivers\\etc\\hosts) was defined local . They have to be equal, otherwise you get the socket.gaieeror.

There seems to be a bug in urllib3 version 1.25.9 package. This produced "socket.gaierror: [Errno 11001] getaddrinfo failed" error for me (working from behind an authenticated proxy server). Downgrading to urllib3 version 1.25.8 solved the problem.

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.

Connect to VPN and then executing the code should work good.

you might did a little mistake in settings.py file.. check your code one more time in your settings file 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

it worked for me. good luck

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM