简体   繁体   English

Python SMTPlib错误111连接被拒绝?

[英]Python SMTPlib Error 111 Connection Refused?

I was using Gmail to send some emails through a program I have hosted on PythonAnywhere with SMTPlib.. It is a single user program so I thought this would work fine. 我使用Gmail通过通过SMTPlib在PythonAnywhere上托管的程序发送一些电子邮件。这是一个单用户程序,所以我认为这样可以正常工作。 However I am running into security issues with Gmail where it will sometimes block the login and not send the mail. 但是,我遇到了Gmail的安全性问题,它有时会阻止登录并且不发送邮件。 So I decided to switch, because I have a website on Hostgator and figured I could just send it through there.. However I am now getting an Error 111 Connection Refused response when trying to send mail. 因此,我决定进行切换,因为我在Hostgator上拥有一个网站,并认为可以直接通过该网站进行发送。但是,现在尝试发送邮件时,我收到了Error 111 Connection Refused响应。 I know for a fact the username/password and SMPT setting are exactly what HostGator recommends for a manual setup.. 我知道事实上,用户名/密码和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()

Here is the full trace-back 这是完整的追溯

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)

Any ideas why this would not work as desired? 任何想法为什么这将无法按需工作? I've also tried using the Non-Secure settings for HostGator and got the same error response. 我还尝试对HostGator使用“非安全”设置,并得到了相同的错误响应。

在此处输入图片说明

You can change you code to: 您可以将代码更改为:

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

Or you can change your SMTP to SMTP_SSL: 或者,您可以将SMTP更改为SMTP_SSL:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM