使用 python 通过 outlook 发送电子邮件时如何解决连接错误?

How do I resolve a connection error when sending an email through outlook with python?

我正在尝试使用 python 发送电子邮件,因为我必须批量发送许多电子邮件,并且希望使用 python 发送电子邮件。我写的代码如下。然而,当我 运行 这个时,我得到以下错误

TimeoutError: [WinError 10060]连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立连接失败,因为连接的主机没有响应

我认为这可能是 Outlook 中设置的问题,但我不确定。

import smtplib

with smtplib.SMTP('smtp.outlook365.com',587) as smtp:
    smtp.ehlo()
    smtp.starttls()
    smtp.ehlo()
    
    smtp.login('email@address.com','password')
    
    subject = 'This is a test.'
    body = 'wonder if this works!'
    
    msg = f'Subject: {subject}\n\n{body}'
    
    smtp.sendmail('sender@email.com','receiver@email.com',msg)

查看smtp服务器地址,参考微软文档看到地址不一样

SMTP 服务器名称 smtp.office365.com

https://support.microsoft.com/en-us/office/pop-imap-and-smtp-settings-for-outlook-com-d088b986-291d-42b8-9564-9c414e2aa040