Python3 SMTP“连接意外关闭”

Lew*_*ers 3 smtp python-3.x

有人可以告诉我为什么我SMTPServerDisconnected("Connection unexpectedly closed")从以下代码中收到错误吗?

import smtplib

from string import Template

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

MY_ADDRESS = '---'
PASSWORD = '---'

def get_contacts(filename):
"""
Return two lists names, emails containing names and email 
addresses
read from a file specified by filename.
"""

names = []
emails = []
with open(filename, mode='r', encoding='utf-8') as contacts_file:
    for a_contact in contacts_file:
        names.append(a_contact.split()[0])
        emails.append(a_contact.split()[1])
return names, emails

def read_template(filename):
"""
Returns a Template object comprising the contents of the 
file specified by filename.
"""

with open(filename, 'r', encoding='utf-8') as template_file:
    template_file_content = template_file.read()
return Template(template_file_content)

def main():
names, emails = get_contacts('contacts.txt') # read contacts
message_template = read_template('message.txt')

# set up the SMTP server
s = smtplib.SMTP('smtp.gmail.com', 465)
s.ehlo()
s.starttls()
s.login(MY_ADDRESS, PASSWORD)

# For each contact, send the email:
for name, email in zip(names, emails):
    msg = MIMEMultipart()       # create a message

    # add in the actual person name to the message template
    message = 
message_template.substitute(PERSON_NAME=name.title())

    # Prints out the message body for our sake
    print(message)

    # setup the parameters of the message
    msg['From']=MY_ADDRESS
    msg['To']=email
    msg['Subject']="This is TEST"
    
    # add in the message body
    msg.attach(MIMEText(message, 'plain'))
    
    # send the message via the server set up earlier.
    s.send_message(msg)
    del msg
    
# Terminate the SMTP session and close the connection
s.quit()

if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)

显然,当我运行代码时,我的地址和密码已被填写。

在终端中运行时我从中得到的跟踪是:

Traceback (most recent call last):
  File "emailAlert2.py", line 71, in <module>
    main()
  File "emailAlert2.py", line 40, in main
    s = smtplib.SMTP('smtp.gmail.com', 465)
  File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 338, in connect
    (code, msg) = self.getreply()
  File "/usr/local/Cellar/python/3.7.5/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
Run Code Online (Sandbox Code Playgroud)

Joh*_*ley 5

Google Gmail 服务器正在挂断您的连接(放弃您的连接尝试)。

如果您已启用对您的 Gmail 帐户的第三方访问(链接),请按如下方式更改您的代码:

s = smtplib.SMTP('smtp.gmail.com', 465)
s.ehlo()
s.starttls()
s.login(MY_ADDRESS, PASSWORD)
Run Code Online (Sandbox Code Playgroud)

改成这样:

s = smtplib.SMTP_SSL('smtp.gmail.com', 465)
s.ehlo()
s.login(MY_ADDRESS, PASSWORD)
Run Code Online (Sandbox Code Playgroud)

挂起的原因是您正在使用未加密的方法 (smtplib.SMTP()) 创建连接。Google 预计您使用需要 SSL 的 SMTPS 进行连接。