Python 3.x 如何在收到Outlook附件后24小时内下载?

Python 3.x 如何在收到Outlook附件后24小时内下载?,python-3.x,email,download,outlook,attachment,Python 3.x,Email,Download,Outlook,Attachment,我正在尝试从收件箱下载所有附件 下载10到20个文件后,程序会抛出一个错误 ,第62行,在 获取附件(原始、电子邮件id) ,第31行,在get_附件中 fileName='{}.format(email_id)+part.get_fileName() TypeError:必须是str,而不是NoneType 另外,我正在尝试更改代码,以便在24小时内从收件箱下载收到的文件并保存在我的下载文件夹中 import imaplib, email, os user = "***" password =

我正在尝试从收件箱下载所有附件

下载10到20个文件后,程序会抛出一个错误

,第62行,在
获取附件(原始、电子邮件id)
,第31行,在get_附件中
fileName='{}.format(email_id)+part.get_fileName()
TypeError:必须是str,而不是NoneType

另外,我正在尝试更改代码,以便在24小时内从收件箱下载收到的文件并保存在我的下载文件夹中

import imaplib, email, os
user = "***"
password = "***"
imap_url = "smtp.outlook.com"
attachment_dir = "/GGG/"
# sets up the auth
def auth(user,password,imap_url):
    con = imaplib.IMAP4_SSL(imap_url)
    con.login(user,password)
    return con
# extracts the body from the email
def get_body(msg):
    if msg.is_multipart():
        return get_body(msg.get_payload(0))
    else:
        return msg.get_payload(None,True)
# allows you to download attachments
def get_attachments(msg,email_id):
    for part in msg.walk():
        if part.get_content_maintype()=='multipart':
            continue
        if part.get('Content-Disposition') is None:
            continue
        fileName = '{} '.format(email_id)+part.get_filename()

        if bool(fileName):
            filePath = os.path.join(attachment_dir, fileName)
            with open(filePath,'wb') as f:
                f.write(part.get_payload(decode=True))
#search for a particular email
def search(key,value,con):
    result, data  = con.search(None,key,'"{}"'.format(value))
    return data
#extracts emails from byte array
def get_emails(result_bytes):
    msgs = []
    for num in result_bytes[0].split():
        typ, data = con.fetch(num, '(RFC822)')
        msgs.append(data)
    return msgs

con = auth(user,password,imap_url)

#All I added is below here
#########################################################
#A method of obtaining inbox size
inbox_size = int(con.select('INBOX')[1][0])

#Here I used a for loop to go through all email ids
for email_id in range(1,inbox_size+1):
    result, data = con.fetch(str(email_id).encode(),'(RFC822)')
    raw = email.message_from_bytes(data[0][1])
    get_attachments(raw,email_id)

根据您的描述,您可以尝试以下代码:

import win32com.client, datetime

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6).Folders('Paper & CD')
messages = inbox.Items
date_now = datetime.datetime.now().date()
date_before = (datetime.datetime.now() + datetime.timedelta(-1)).date()
messages.Find("[Start] >= """ & date_before & """ and [Start] <= """ & date_now & """")

for msg in messages:        
    for att in msg.Attachments:
        if att.FileName == 'list.csv': 
            att.SaveAsFile('C:\\My\\temp\\' + msg.subject + att.FileName)
        att.SaveAsFile('C:\\My\\temp\\' + att.FileName)
import win32com.client,datetime
outlook=win32com.client.Dispatch(“outlook.Application”).GetNamespace(“MAPI”)
收件箱=outlook.GetDefaultFolder(6).文件夹('纸张和CD')
邮件=收件箱。邮件
date\u now=datetime.datetime.now().date()
date_before=(datetime.datetime.now()+datetime.timedelta(-1)).date()

messages.Find(“[Start]>=”)&date\u before&”和[Start]是否检查此链接:@AlinaLi感谢您指出。我运行了mahendra prabu发布的代码。它抛出了一个错误“self未定义”“。尝试过调试,但没有成功。代码中也没有解释,因此很难理解。@AlinaLi我正在使用Mac,win32.client不可用。有点卡住。谢谢你的回答。你能告诉我它是在Linux还是Mac上工作吗?你可以在Mac上使用它。请参阅此链接: