Python SMTPlib错误111连接被拒绝?

时间:2017-02-13 11:45:01

标签: python email smtp smtplib pythonanywhere

我使用Gmail通过我在PythonAnywhere上使用SMTPlib托管的程序发送一些电子邮件。这是一个单用户程序,所以我认为这样可以正常工作。但是我遇到了Gmail的安全问题,它有时会阻止登录而不会发送邮件。所以我决定切换,因为我在Hostgator上有一个网站,并认为我可以通过那里发送..但是我现在在尝试发送邮件时收到错误111连接拒绝响应。我知道一个事实,用户名/密码和SMPT设置正是HostGator推荐用于手动设置的。

def sendmail(toaddr, msg):
    fromaddr = 'john@usmith.com'
    cc = 'chris@*************.com'
    msg['Cc'] = cc
    server = smtplib.SMTP('gator3119.hostgator.com', 465)
    server.login(fromaddr, "*******")
    text = msg.as_string()
    server.sendmail(fromaddr, [toaddr, cc], text)
    server.quit()

这是完整的追溯

2017-02-13 11:31:40,274 :ConnectionRefusedError: [Errno 111] Connection refused
     File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1836, in __call__
2017-02-13 11:31:40,274 :    return self.wsgi_app(environ, start_response)
2017-02-13 11:31:40,274 :
2017-02-13 11:31:40,274 :  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1820, in wsgi_app
2017-02-13 11:31:40,274 :    response = self.make_response(self.handle_exception(e))
2017-02-13 11:31:40,274 :
2017-02-13 11:31:40,274 :  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1403, in handle_exception
2017-02-13 11:31:40,274 :    reraise(exc_type, exc_value, tb)
2017-02-13 11:31:40,275 :
2017-02-13 11:31:40,275 :  File "/usr/local/lib/python3.5/dist-packages/flask/_compat.py", line 33, in reraise
2017-02-13 11:31:40,275 :    raise value
2017-02-13 11:31:40,275 :
2017-02-13 11:31:40,275 :  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1817, in wsgi_app
2017-02-13 11:31:40,275 :    response = self.full_dispatch_request()
2017-02-13 11:31:40,275 :
2017-02-13 11:31:40,275 :  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1477, in full_dispatch_request
2017-02-13 11:31:40,275 :    rv = self.handle_user_exception(e)
2017-02-13 11:31:40,275 :
2017-02-13 11:31:40,275 :  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1381, in handle_user_exception
2017-02-13 11:31:40,275 :    reraise(exc_type, exc_value, tb)
2017-02-13 11:31:40,275 :
2017-02-13 11:31:40,275 :  File "/usr/local/lib/python3.5/dist-packages/flask/_compat.py", line 33, in reraise
2017-02-13 11:31:40,275 :    raise value
2017-02-13 11:31:40,275 :
2017-02-13 11:31:40,276 :  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1475, in full_dispatch_request
2017-02-13 11:31:40,276 :    rv = self.dispatch_request()
2017-02-13 11:31:40,276 :
2017-02-13 11:31:40,276 :  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1461, in dispatch_request
2017-02-13 11:31:40,276 :    return self.view_functions[rule.endpoint](**req.view_args)
2017-02-13 11:31:40,276 :
2017-02-13 11:31:40,276 :  File "/home/chrisutpg/utpp/UTPP/notes.py", line 29, in addnote
2017-02-13 11:31:40,276 :    sendNote(noteInfo, clientInfo)
2017-02-13 11:31:40,276 :
2017-02-13 11:31:40,276 :  File "/home/chrisutpg/utpp/UTPP/uttpemail.py", line 192, in sendNote
2017-02-13 11:31:40,276 :    sendmail(toaddr, msg)
2017-02-13 11:31:40,276 :
2017-02-13 11:31:40,276 :  File "/home/chrisutpg/utpp/UTPP/uttpemail.py", line 16, in sendmail
2017-02-13 11:31:40,276 :    server = smtplib.SMTP('gator3119.hostgator.com', 465)
2017-02-13 11:31:40,276 :
2017-02-13 11:31:40,277 :  File "/usr/lib/python3.5/smtplib.py", line 251, in __init__
2017-02-13 11:31:40,277 :    (code, msg) = self.connect(host, port)
2017-02-13 11:31:40,277 :
2017-02-13 11:31:40,277 :  File "/usr/lib/python3.5/smtplib.py", line 335, in connect
2017-02-13 11:31:40,277 :    self.sock = self._get_socket(host, port, self.timeout)
2017-02-13 11:31:40,277 :
2017-02-13 11:31:40,277 :  File "/usr/lib/python3.5/smtplib.py", line 306, in _get_socket
2017-02-13 11:31:40,277 :    self.source_address)
2017-02-13 11:31:40,277 :
2017-02-13 11:31:40,277 :  File "/usr/lib/python3.5/socket.py", line 711, in create_connection
2017-02-13 11:31:40,277 :    raise err
2017-02-13 11:31:40,277 :
2017-02-13 11:31:40,277 :  File "/usr/lib/python3.5/socket.py", line 702, in create_connection
2017-02-13 11:31:40,277 :    sock.connect(sa)

为什么这不符合要求的任何想法?我也尝试使用HostGator的非安全设置并得到相同的错误响应。

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以将代码更改为:

server = smtplib.SMTP('gator3119.hostgator.com:465')
server.starttls()
server.login(fromaddr, "*******")

或者您可以将SMTP更改为SMTP_SSL:

server = smtplib.SMTP_SSL('gator3119.hostgator.com:465')
server.login(fromaddr, "*******")