Question

[Solved] 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:Python27libsmtplib.py", line 249, in __init__
    (code, msg) = self.connect(host, port)
  File "C:Python27libsmtplib.py", line 309, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:Python27libsmtplib.py", line 284, in _get_socket
    return socket.create_connection((port, host), timeout)
  File "C:Python27libsocket.py", line 553, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11001] getaddrinfo failed
Solution #1:

I know for sure that gaierror comes up when you are working from behind proxy.

Respondent: Deepak Saini
Solution #2:

The problem is that the DNS lookup for ‘smtp server name’ is failing – if this is your exact code then you can see why – if not and you have the valid qualified name for the SMTP server then you may have issues with the firewall/internet connection, etc., also port has to be set to a valid value to match your servers SMTP configuration, (usually port 25 but not absolutely always).

Respondent: Steve Barnes
Solution #3:

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:windowssystem32driversetchosts) was defined local.
They have to be equal, otherwise you get the socket.gaieeror.

Respondent: FunCoding
Solution #4:

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.

Respondent: lmocsi
Solution #5:

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.

Respondent: Tad
Solution #6:

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
Respondent: Naresh Chary
Solution #7:

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"
Respondent: Ras
Solution #8:

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

Respondent: Gray Hat
Solution #9:

You need to activate IMAP/SMTP service active for your host mail.

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy