gpt4 book ai didi

python - 使用带有 365 电子邮件地址的 SMTPLIB SSL 电子邮件时出错

转载 作者:太空宇宙 更新时间:2023-11-03 14:22:06 25 4
gpt4 key购买 nike

context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.office365.com", 587, context=context) as server:

(587) 当我运行它时,我收到一个 SSL 错误:[SSL: WRONG_VERSION_NUMBER] 错误的版本号 (_ssl.c:1056)。

(465) 我收到超时错误。

我尝试使用端口 465 和 587。当我使用不同的端口时,我会收到不同的错误。我确实尝试了 995 只是为了它,但仍然没有运气。如果我使用我的 gmail 帐户,我没有问题。

我需要对我的电子邮件帐户做些什么才能正常工作吗?我也试过 .SMTP() 但还是不行。

smtp = smtplib.SMTP("smtp.office365.com",587)
context = ssl.create_default_context()
with smtp.starttls(context=context) as server:
server.login(from_address, password)

for i, r in newhire[mask].iterrows():
server.sendmail(
from_address,
r["Email"],
message.format(Employee=r["Employee Name"],
StartDate=r["StartDate"],
PC=r["PC"],
Title=r["Title"],
Email=r["Email"],


)
)

最佳答案

来自 the documentation of SMTP_SSL :

SMTP_SSL should be used for situations where SSL is required from the beginning of the connection and using starttls() is not appropriate.

因此,SMTP_SSL 用于隐式 SMTP,其通用端口为 465。端口 587 用于显式 SMTP,其中完成普通连接,稍后使用 STARTTLS 命令升级到 SSL。

这里发生的事情是,客户端尝试与在此阶段不期望 SSL/TLS 的服务器对话 SSL/TLS,因此使用非 TLS 数据进行回复。尽管如此,它们仍被解释为 TlS,这导致了这个奇怪的 [SSL: WRONG_VERSION_NUMBER]

要解决此问题,请将端口 465(而不是 587)与 SMTP_SSL(Office365 不支持)一起使用,或者使用端口 587 但与 starttls 一起使用:

with smtplib.SMTP("smtp.office365.com", 587) as server:
server.starttls(context=context)

关于python - 使用带有 365 电子邮件地址的 SMTPLIB SSL 电子邮件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59635516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com