提问人:Bird 提问时间:11/17/2023 最后编辑:Bird 更新时间:11/19/2023 访问量:41
为什么我的 Python SMTPLib 电子邮件脚本不起作用,我该如何解决?
Why is my Python SMTPLib email script not working and how do I fix it?
问:
因此,我使用Youtube教程编写了一个基本的Python电子邮件脚本。评论说视频很有帮助,但我的代码不起作用。
我的代码如下所示
from email.message import EmailMessage
import ssl
import smtplib
email_sender = '[email protected]'
email_password = "corr ectp assw ords"
email_receiver = '[email protected]'
subject = 'Written by Code!'
body = """
This was written by my program! Tell me if it worked.
"""
em = EmailMessage()
em['From'] = email_sender
em['To'] = email_receiver
em['Subject']
em.set_content(body)
context = ssl.create_default_context()
with smtplib.SMTP_SSL('smtp.gmail.com', 465, context) as smtp:
smtp.login(email_sender, email_password)
smtp.sendmail(email_sender, email_receiver, em.as_string())
错误如下所示:smtplib.SMTPServerDisconnected: Connection unexpectedly closed
有谁知道它为什么失败以及我该如何修复它?谢谢!
答:
0赞
Bird
11/17/2023
#1
所以我解决了我的问题。我不得不切换smtplib.SMTP_SSL('smtp.gmail.com', 465, context)
smtplib.SMTP_SSL('smtp.gmail.com')
我不知道它为什么有效
评论
"stmp.gmail.com"
smtp
smtplib.SMTPServerDisconnected: Connection unexpectedly closed