对于 Office 365 电子邮件设置,POP 的端口似乎是 903。您可以尝试使用 Microsoft365 下的 Web 设置,也可以尝试使用 IMAP 获取您的电子邮件。
只是为了节省您的时间,我写了一些使用 imap 获取您的电子邮件的工作行和一个使用 smtp 发送的简单代码
import smtplib
import imaplib
import ssl
def send_email():
send_port = 587
with smtplib.SMTP('smtp.office365.com', send_port) as smtp:
#Need a ehlo message before starttls
smtp.ehlo()
smtp.starttls()
# Server login
smtp.login(<user-email>, <password>)
# Email data
sender_email = <user-email>
receiver_email = <destination-email>
email_subject = <some-subjetct>
email_body = <some-body>
message = 'Subject: {}\n\n{}'.format(email_subject, email_body)
smtp.sendmail(sender_email, receiver_email, message)
smtp.quit()
def receive_email():
receive_port = 993
with imaplib.IMAP4_SSL('outlook.office365.com', receive_port) as imap:
# Server login
imap.login(<youremail>, <password>)
# Get the list of folders in you email account and some extra info.
retcode, folders = imap.list()
# status of the fetch
print ('Response code:', retcode)
# folders and info of hierarchies
print ('Response code:', folders)
imap.close()
#Test
send_email()
receive_email()
请注意,此代码需要在防火墙上打开 imap 和 smtp 端口。也使用与使用https://www.office.com/相同的用户电子邮件和密码