Outlook 根据日期、发件人和主题行使用 MAPI python 下载附件
Outlook download attachments with MAPI python based on date, sender and subject line
我完全不熟悉我正在尝试根据日期、发件人的电子邮件地址和主题行使用 MAPI 下载附件。
下面是我的代码:
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)
但是,显示如下错误:
today= msg.Senton.date() == val_date
AttributeError: 'str' object has no attribute 'Senton'
请帮忙!
首先,这不是 MAPI - 这是 Outlook 对象模型。其次,您假设收件箱文件夹中只有 MailItem
个对象 - 但您也可以有 ReportItem
、MeetingItem
等。首先确保您确实有一个 MailItem
对象 - 检查是否 Class
属性 == 43 (olMailItem
)
我有同样的错误信息,但发现这是我的语法。改变
Senton
到 SentOn
清除了错误。
我完全不熟悉我正在尝试根据日期、发件人的电子邮件地址和主题行使用 MAPI 下载附件。
下面是我的代码:
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)
但是,显示如下错误:
today= msg.Senton.date() == val_date
AttributeError: 'str' object has no attribute 'Senton'
请帮忙!
首先,这不是 MAPI - 这是 Outlook 对象模型。其次,您假设收件箱文件夹中只有 MailItem
个对象 - 但您也可以有 ReportItem
、MeetingItem
等。首先确保您确实有一个 MailItem
对象 - 检查是否 Class
属性 == 43 (olMailItem
)
我有同样的错误信息,但发现这是我的语法。改变
Senton
到 SentOn
清除了错误。