电子邮件读取代码在公司系统中不起作用

时间:2019-04-03 04:53:45

标签: python python-3.x email imap

我正在使用以下代码从邮件服务器中检索电子邮件:

import email
import imaplib
from bs4 import BeautifulSoup

user='XXXXXXXX@YYYY.com'
password='PASSWORD'
mail = imaplib.IMAP4_SSL('SERVER_NAME',PORT)
mail.login(user,password)

mail.select('inbox')
result,data=mail.uid('search',None,'(HEADER Subject "Reply Of Confirmation") UNSEEN')
inbox_item_list=data[0].split()
res=None
for num in inbox_item_list:
      result1,email_data=mail.uid('fetch',num,'RFC822')
      #print(email_data)
      email_message = email.message_from_string(email_data[0][1].decode("utf-8"))
      #print(dir(email_message))
      if email_message.is_multipart():
          for payload in email_message.get_payload():
              if payload.get_content_maintype() == 'text':
                  soup = BeautifulSoup(payload.get_payload(),"html.parser")
                  text = soup.get_text()
                  res=text
                  break
      else:
          soup = BeautifulSoup(payload.get_payload())
          text = soup.get_text()
          res=text
          break
print("Content-type:text/html\r\n\r\n")
print(res)

此代码已在我的个人笔记本电脑上成功运行,但是在公司系统上运行时会出现此错误:

 sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

我该如何解决?

这是完整的错误回溯:

Traceback (most recent call last):
  File "C:\Users\1SATAVHI\Desktop\gmail.py", line 8, in <module>
    mail = imaplib.IMAP4_SSL('imap-eu.dc.ege.ds',993)
  File "C:\Users\1SATAVHI\AppData\Local\Programs\Python\Python36\lib\imaplib.py", line 1283, in __init__
    IMAP4.__init__(self, host, port)
  File "C:\Users\1SATAVHI\AppData\Local\Programs\Python\Python36\lib\imaplib.py", line 197, in __init__
    self.open(host, port)
  File "C:\Users\1SATAVHI\AppData\Local\Programs\Python\Python36\lib\imaplib.py", line 1296, in open
    IMAP4.open(self, host, port)
  File "C:\Users\1SATAVHI\AppData\Local\Programs\Python\Python36\lib\imaplib.py", line 294, in open
    self.sock = self._create_socket()
  File "C:\Users\1SATAVHI\AppData\Local\Programs\Python\Python36\lib\imaplib.py", line 1286, in _create_socket
    sock = IMAP4._create_socket(self)
  File "C:\Users\1SATAVHI\AppData\Local\Programs\Python\Python36\lib\imaplib.py", line 284, in _create_socket
    return socket.create_connection((self.host, self.port))
  File "C:\Users\1SATAVHI\AppData\Local\Programs\Python\Python36\lib\socket.py", line 722, in create_connection
    raise err
  File "C:\Users\1SATAVHI\AppData\Local\Programs\Python\Python36\lib\socket.py", line 713, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

1 个答案:

答案 0 :(得分:0)

imaplib.IMAP4_SSL('imap-eu.dc.ege.ds',993)尝试连接到DNS中不存在的主机。