3

I am trying to send mail using smtplib module. I was able to successfully send email using my Windows-7 PC, but while I was trying the same code in Windows Server 2008 R2 Enterprise 64-bit(6.1, Build 7601) I am getting TimeoutError: [WinError 10060].

Code attached below.

def send_email(from_addr, to_addr_list,
              subject, html_body,plain_text_body,
              login,
              password,
              smtpserver='smtp.gmail.com:587',
              cc_addr_list=None,
              attachment=None,
              from_name=None):

    message=MIMEMultipart()

    plain=MIMEText(plain_text_body,'plain')
    #html=MIMEText(html_body,'html') 

    message.add_header('from',from_name)
    message.add_header('to',','.join(to_addr_list))
    message.add_header('subject',subject)

    if attachment!=None:
        attach_file=MIMEApplication(open(attachment,"rb").read())
        attach_file.add_header('Content-Disposition','attachment; filename="%s"' % attachment)
        message.attach(attach_file)


    message.attach(plain)
    #message.attach(html)

    server = smtplib.SMTP(smtpserver) #TimeoutError raised on this line 
    server.starttls()
    server.login(login,password)
    server.sendmail(from_addr, to_addr_list, message.as_string())
    server.quit()
    print("MailSent")

I am getting error in below line

server = smtplib.SMTP(smtpserver)

Error message attached below

"C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 336, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 307, in _get_socket
    self.source_address)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\socket.py", line 728, in create_connection
    raise err
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\socket.py", line 716, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

I have tried all the below options, I am getting the same TimeoutError: [WinError 10060];

  1. I have enabled "Allow Less Secure Apps" option in Gmail account.
  2. Server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
  3. Server = smtplib.SMTP ("smtp.gmail.com", 587)
  4. Server = smtplib.SMTP ("smtp.gmail.com", 587, timeout=120)
  5. Server = smtplib.SMTP ("smtp.gmail.com:587”)
  6. I have allowed port 587 and 465 in firewall Inbound and Outbound rules.
  7. I have also tried yagmail module (I am getting same error)

Am I missing something? or any alternative solution much appreciated.

Bhuvan Kumar
  • 554
  • 1
  • 7
  • 23
  • 1
    did you try `telnet smtp.gmail.com 587` or similar connectivity tools, which would provide an overview of whether the gmail's smtp server is able to connect from that Windows Server? – Nagaraj Tantri Aug 29 '20 at 00:49
  • In Windows Server i am able to login to gmail from Chrome. – Bhuvan Kumar Aug 29 '20 at 07:34
  • Hey Bhuvan, Chrome uses port 80/443 (https/http) to connect to gmail. Can you confirm with the above command? I mean, can you run that in your terminal? maybe you have to install telnet client once. – Nagaraj Tantri Aug 30 '20 at 00:15
  • What do you have in to_addr_list? – RafaelJan Sep 02 '20 at 10:03
  • For testing From and To both are same. – Bhuvan Kumar Sep 02 '20 at 14:10
  • I am getting the same error. May I know how you have solved it. –  Jul 24 '21 at 07:12
  • It was a computer permission problem, Then I tried with local mail server worked for me, below link might help. `https://stackoverflow.com/questions/3362600/how-to-send-email-attachments/3363254#336325` – Bhuvan Kumar Jul 24 '21 at 12:49

0 Answers0