2017-05-08 289 views
1

我有以下脚本:脚本在python,下载邮件附件

import imaplib 
import email 
import os 

svdir = 'c:/downloads' 

mail = imaplib.IMAP4('https://outlook.office365.com/mapi/emsmdb/?' 
        '[email protected]') 
mail.login("RNa", "RN!") 
mail.select("ADP Files") 

typ, msgs = mail.search(None, '(SUBJECT "ADP Files")') 
msgs = msgs[0].split() 

for emailid in msgs: 
    resp, data = mail.fetch(emailid, "(RFC822)") 
    email_body = data[0][1] 
    m = email.message_from_string(email_body) 

    if m.get_content_maintype() != 'multipart': 
     continue 

    for part in m.walk(): 
     if part.get_content_maintype() == 'multipart': 
      continue 
     if part.get('Content-Disposition') is None: 
      continue 

     filename = part.get_filename() 
     if filename is not None: 
      sv_path = os.path.join(svdir, filename) 
      if not os.path.isfile(sv_path): 
       print(sv_path) 
       fp = open(sv_path, 'wb') 
       fp.write(part.get_payload(decode=True)) 
       fp.close() 

中产生的错误是:

C:\Users\rnandipati\Desktop\Batch Files\EMAIL ADP>email.py 

Traceback (most recent call last): 
    File "C:\Python34\lib\encodings\idna.py", line 165, in encode 
    raise UnicodeError("label empty or too long") UnicodeError: label empty or too long 

The above exception was the direct cause of the following exception: 
Traceback (most recent call last): 
    File "C:\Users\rnandipati\Desktop\Batch Files\EMAIL ADP\email.py", line 2, in <module> 
     import email 
    File "C:\Users\rnandipati\Desktop\Batch Files\EMAIL ADP\email.py", line 8, in <module> 
     mail=imaplib.IMAP4('https://outlook.office365.com/mapi/emsmdb/[email protected]') 
    File "C:\Python34\lib\imaplib.py", line 181, in __init__ 
     self.open(host, port) 
    File "C:\Python34\lib\imaplib.py", line 257, in open 
     self.sock = self._create_socket() 
    File "C:\Python34\lib\imaplib.py", line 247, in _create_socket 
     return socket.create_connection((self.host, self.port)) 
    File "C:\Python34\lib\socket.py", line 494, in create_connection 
     for res in getaddrinfo(host, port, 0, SOCK_STREAM): 
    File "C:\Python34\lib\socket.py", line 533, in getaddrinfo 
     for res in _socket.getaddrinfo(host, port, family, type, proto, flags): UnicodeError: encoding with 'idna' codec failed (UnicodeError: label empty or to o long) 

我不知道为什么这个错误产生的。我对这种脚本很陌生。

回答

0

根据文档(和源代码太),构造imaplib.IMAP4()两个参数:主机:主机名(通常是一个服务器名或IP地址),默认为“本地主机”,和端口 :端口号,默认为标准IMAP4端口)。

在这里,您使用的URL是错误的。

尝试打开这样的连接:

with imaplib.IMAP4("outlook.office365.com") as imap4: 
    ... 

咨询此页拥有Office 360​​的连接设置:POP and IMAP settings for Outlook Office 365 for business

您可能需要为Outlook办公室SSL连接365这里是一个更有用的使用示例:

import imaplib 

username = '[email protected]' 
password = 'password' 

with imaplib.IMAP4_SSL('outlook.office365.com') as imap4: 
    imap4.login(username, password) 

    # retrieve a list of the mailboxes and select one 
    result, mailboxes = imap4.list() 
    imap4.select("inbox") 

编辑:选择对象不with声明

try: 
    imap4.login(username, password) 

    # retrieve a list of the mailboxes and select one 
    result, mailboxes = imap4.list() 
    imap4.select("inbox") 

finally: 
    imap4.close() 
+0

当我连接使用SSL它显示我AttributeError的:___exit___ .. @Laurent LAPORTE – Rahul

+0

你当然使用Python 2.不要使用**有**声明。 –

+0

我正在使用python 3.4 @Laurent LAPORTE – Rahul