尝试使用Python的poplib连接到Outlook时出现连接拒绝错误

时间:2020-03-27 19:44:19

标签: python-3.x outlook pop3 poplib

我正在尝试使用python中的POP3连接到Outlook。

mailbox = poplib.POP3_SSL('outlook.office365.com', 995)

我收到以下错误:

[WinError 10061] No connection could be made because the target machine actively refused it

我正在使用VPN,并且可以ping 'outlook.office365.com'而没有问题。我还尝试了'pop-mail.outlook.com''pop3.live.com',因为我看到它们被称为在线主机名,并且收到了相同的错误。请让我知道是否还有其他有用的信息。

1 个答案:

答案 0 :(得分:0)

对于Office 365电子邮件settings,似乎POP的端口是903。您可以尝试使用Microsoft365下的网络中的设置来尝试使用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/

相同的用户电子邮件和密码