如何连接到 Microsoft Exchange Smtp?

How to connect to Microsoft Exchange Smtp?

通常,对于 Gmail 和 Outlook,我们有连接到 SMTP 的应用密码(尽管我们可以通过启用不太安全的应用访问来使用电子邮件密码)。

但是对于我的 Microsoft Exchange 邮件,我找不到应用程序密码,也无法将我的应用程序与邮件连接以发送电子邮件。

import smtplib
password = "mypassword"
email = "usrname@tsac-uae.com"
s = smtplib.SMTP('smtp.office365.com', 587)
s.starttls()
s.login(email, password)

我收到一条错误消息

smtplib.SMTPAuthenticationError: (535, b'5.7.139 Authentication unsuccessful, 
SmtpClientAuthentication is disabled for the Tenant. 
Visit https://aka.ms/smtp_auth_disabled for more information. 
[AM9P192CA0015.EURP192.PROD.OUTLOOK.COM]')

为了避免这些并发症,我需要使用应用密码。但我找不到如何设置应用程序密码作为交换。另外,网站上说的步骤已经过时了。我在任何地方都找不到启用 SMTP。

您需要启用 SMTP 提交(组织范围的设置) 根据 link 在您的 Microsoft 租户中,您收到错误消息,该消息扩展为 https://docs.microsoft.com/en-us/exchange/clients-and-mobile-in-exchange-online/authenticated-client-smtp-submission.

更改只能通过 powershell 执行。执行此更改的一种简单方法是在 https://shell.azure.com 登录。要在 Azure 云 Shell 中使用 Exchange Online cmdlet,需要使用 Connect-EXOPSSession 导入 Exchange Online cmdlet。 连接后,要启用 SMTP 提交,请使用

Set-TransportConfig -SmtpClientAuthenticationDisabled $false 

请务必阅读上面 link 的文章以了解安全隐患。 还要注意

If your authentication policy disables basic authentication for SMTP, clients cannot use the SMTP AUTH protocol even if you enable the settings outlined in this article. For more information, see Disable Basic authentication in Exchange Online.