简体   繁体   中英

Outlook download attachments with MAPI python based on date, sender and subject line

I am completely new to I am trying to download attachments using MAPI based on date, sender's email address and subject line.

Below is my code:

import datetime 
from win32com.client import Dispatch
outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder("6")
all_inbox = inbox.Items
folders = inbox.Folders
val_date = datetime.date.today().strftime("%d-%m-&y")
today = msg.Senton.date() = val_date
inboxtime = all_inbox and today
email_sender = 'Email input here'
sub_today = 'Subject line input here'
att_today = 'attachment name input here'

for msg in inboxtime:
    if msg.SenderEmailType == "EX":
       if msg.Sender.GetExchangeUser().PrimarySmtpAddress.find(email_sender) != -1
           break
else:
       if msg.SenderEmailAddress.find(email_sender) != -1 and msg.Senton.date() == val_date:
          break
For att in msg.Attachments:
if att.FileName == att_today:
   break
try:
    att.SaveAsFile('D:\\' + att.FileName)
    print(True)
except:
    print(False)

However, it shows below error:

today= msg.Senton.date() == val_date
AttributeError: 'str' object has no attribute 'Senton'

Please help!

Firstly, this is not MAPI - this is Outlook Object Model. Secondly, you are assuming there are only MailItem objects in the Inbox folder - but you can also have ReportItem , MeetingItem , etc. Make sure first that you really have a MailItem object - check if the Class property == 43 ( olMailItem )

I had the same error message, but found out it was my syntax. Changing Senton to SentOn cleared the error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM