【问题标题】:Connection Refused Error When Trying to Connect to Outlook Using Python's poplib尝试使用 Python 的 poplib 连接到 Outlook 时出现连接被拒绝错误
【发布时间】:2020-03-27 19:44:19
【问题描述】:

我正在尝试在 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',因为我在网上看到它们被称为主机名,并且收到了同样的错误。如果我能提供更多有用的信息,请告诉我。

【问题讨论】:

    标签: python-3.x outlook pop3 poplib


    【解决方案1】:

    对于 office 365 电子邮件settings,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/ 相同的用户电子邮件和密码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多