TimeoutError:使用smtplib时,Windows Server中出现[WinError 10060]

时间:2020-08-23 15:25:11

标签: python email gmail smtplib

我正在尝试使用smtplib模块发送邮件。我可以使用Windows-7 PC成功发送电子邮件,但是在Windows Server 2008 R2 Enterprise 64-bit(6.1, Build 7601)中尝试相同的代码时却收到了TimeoutError: [WinError 10060]

下面附有代码。

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")

我在下面的行中出现错误

server = smtplib.SMTP(smtpserver)

下面附有错误消息

"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

我尝试了以下所有选项,我得到的是相同的TimeoutError: [WinError 10060];

  1. 我已在Gmail帐户中启用了“允许安全程度较低的应用程序”选项。
  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. 我在防火墙的入站和出站规则中允许端口587和465。
  7. 我还尝试了yagmail模块(出现相同错误)

我错过了什么吗?或任何其他值得赞赏的解决方案。

0 个答案:

没有答案