SMTPServer断开连接:连接意外关闭

时间:2018-12-04 06:40:09

标签: python smtp

这是我的代码,我不知道为什么服务器一直意外关闭。我知道登录凭据绝对正确,但是如果输入错误,它将无法正确连接到getgo?那为什么会意外关闭呢?我感觉我没有使用正确的端口?我也尝试过使用server.startTLS(),但仍然不起作用:

#Send Email
server = smtplib.SMTP('smtp.gmail.com', 465)
server.connect('smtp.gmail.com', 465)
server.ehlo()
server.login("username@gmail.com","password123")
server.send_message("Hello", "john@gmail.com", "crabbys@gmail.com");

以下是我得到的确切错误消息:

File "/Users/neel/Documents/CSE5911-TAXI-APP/env/lib/python3.7/site-        packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
  File "/Users/neel/Documents/CSE5911-TAXI-APP/env/lib/python3.7/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
  File "/Users/neel/Documents/CSE5911-TAXI-APP/env/lib/python3.7/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
  File "/Users/neel/Documents/CSE5911-TAXI-APP/env/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
raise value
  File "/Users/neel/Documents/CSE5911-TAXI-APP/env/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
  File "/Users/neel/Documents/CSE5911-TAXI-APP/env/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
  File "/Users/neel/Documents/CSE5911-TAXI-APP/env/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
  File "/Users/neel/Documents/CSE5911-TAXI-APP/env/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
raise value
  File "/Users/neel/Documents/CSE5911-TAXI-APP/env/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
  File "/Users/neel/Documents/CSE5911-TAXI-APP/env/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/neel/Documents/CSE5911-TAXI-APP/flaskr/taxi_api.py", line 44, in taxi_request
server = smtplib.SMTP('smtp.gmail.com', 465)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 338, in connect
(code, msg) = self.getreply()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 394, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

1 个答案:

答案 0 :(得分:0)

你可以试试smtplib.SMTP_SSL('smtp.gmail.com',465)