将SMTPLIB SSL电子邮件与365电子邮件地址一起使用时出错 [英] Errors when using SMTPLIB SSL email with a 365 email address

查看:318
本文介绍了将SMTPLIB SSL电子邮件与365电子邮件地址一起使用时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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).

(587) When I run this I get an SSL error: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056).

(465)我收到超时错误.

(465) I get a timeout error.

我尝试使用端口465和587.当我使用不同的端口时,会遇到不同的错误.我确实尝试过995,但仍然没有运气.如果我使用Gmail帐户,则没有任何问题.

I tried using ports 465 and 587. I get different errors when I use different ports. I did try 995 just for the heck of it and still no luck. If I use my gmail account, I have no issues.

我是否需要对我的电子邮件帐户进行处理才能正常工作.我还尝试了.SMTP(),但还是没有运气.

Is there something I need to do to my email account so it works. I also tried .SMTP() and still no luck.

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"], 


                )
            )

推荐答案

来自

在从连接开始就需要SSL且不适合使用starttls()的情况下,应使用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,在该SMTP中,进行普通连接,然后使用STARTTLS命令升级到SSL.

Thus, SMTP_SSL is for implicit SMTP and the common port for this is 465. Port 587 is instead used for explicit SMTP where a plain connect is done and later an upgrade to SSL with the STARTTLS command.

这里发生的是,客户端尝试与不希望SSL/TLS的服务器通话SSL/TLS,因此将使用非TLS数据进行答复.尽管如此,这些仍然被解释为Tls,这导致了这个奇怪的[SSL: WRONG_VERSION_NUMBER].

What happens here is that the client tries to speak SSL/TLS to a server which does not expect SSL/TLS at this stage and thus replies with non-TLS data. These get interpreted as TlS nonetheless which results in this strange [SSL: WRONG_VERSION_NUMBER].

要解决此问题,请使用端口465(而不是587)和SMTP_SSL(不受Office365支持)或使用端口587,但要使用

To fix this either use port 465 (and not 587) with SMTP_SSL (not supported by Office365) or use port 587 but with starttls:

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

这篇关于将SMTPLIB SSL电子邮件与365电子邮件地址一起使用时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆