使用 tls/ssl 使用 python 为 office365 发送 SMTP 电子邮件

send SMTP email for office365 with python using tls/ssl

我正在尝试创建一个 python 脚本来发送包含一些消息的电子邮件。 我的设置基于 smtp.office365.com。我创建了一些脚本来完成这项工作。

#!/usr/bin/python

print ("HI! let's start")  
import smtplib  
import getpass

port=587                                                                                                                    
smtp_server="smtp.office365.com"  
sender_email='mymail@something.com'  
receiver_email='mymail@something.com'  
print ("defining server")  
server = smtplib.SMTP(smtp_server,port)

password=getpass.getpass("enter password")

message='''
Hi,
THIS IS A TEST MESSAGE.
'''

print ("logging in")  
server.login(sender_email,password)  
print ("sending mail now")  
server_sendmail(sender_email,receiver_email,message)  
print ("all must be done by now")

但是由于某些原因,打印出来后就卡住了"defining server"。永远不会要求输入密码。显然 smtplib.SMTP 命令挂起。你能说出为什么会发生这种情况以及如何摆脱它吗?我的设置如下:

Server: smtp.office365.com
/sjmail.com
security : STARTTLS
Port : 587

Vishesh 艾莉亚!

Office 365 帐户不支持 SMTP AUTH 扩展。搜索 Microsoft Graph 来执行此操作。