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]
;
- I have enabled "Allow Less Secure Apps" option in Gmail account.
Server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
Server = smtplib.SMTP ("smtp.gmail.com", 587)
Server = smtplib.SMTP ("smtp.gmail.com", 587, timeout=120)
Server = smtplib.SMTP ("smtp.gmail.com:587”)
- I have allowed port 587 and 465 in firewall Inbound and Outbound rules.
- I have also tried
yagmail
module (I am getting same error)
Am I missing something? or any alternative solution much appreciated.